Python Forum
How to fix 'uncaught exception of type NSException' in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fix 'uncaught exception of type NSException' in Python
#1
I'm currently making a database using MySql in Python in order to collect information about hospitals. I've created different programs (like one for the registration form, one for the database, and so on.)
and imported them (To execute them) in various parts of my other programs. However, one of them does not perform its function and raises an error.

Here is the overall procedure:

. Make a program to create a database using MySql
. Make a program to create 2 buttons using Pygame (Register and login)
. Make the program to create a registration form when Register is clicked.
. Send the details of it to the database

Overall, the program works if the Pygame buttons program isn't included. The Database program executes the Registration form program, which obtains the details entered, and is sent by the Database programme to the database created. However, executing the Registration Form when I click on the Register button (in the Buttons program) raises an error(uncaught exception of type NSException') and python closes. I've tried many ways of doing the same, even of copy-pasting the code into the program itself, but they both yield the same result.

Could anyone help to correct this error
Here is the code I've used for the Button:
import pygame
pygame.init()
class button():
    def __init__(self, colour, x, y, width, height, text=''):
        self.colour = colour
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self, win, outline=None):
        if outline:
            pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)

        pygame.draw.rect(win, self.colour, (self.x, self.y, self.width, self.height), 0)

        if self.text != '':
            font = pygame.font.SysFont('comicsans', 35)
            text = font.render(self.text, 1, (0, 0, 0))
            win.blit(text, (
            self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))

    def isOver(self, pos):
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True

        return False


def redrawGameWindow():
    win.fill((255, 255, 255))
    loginButton.draw(win)
    registerButton.draw(win)


def register():
    import Databases



run = True
loginButton = button((0, 255, 0), 175, 150, 150, 60, 'Login')
registerButton = button((255, 0, 0), 175, 300, 150, 60, 'Register')
while run:
    redrawGameWindow()
    pygame.display.update()

    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            quit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if loginButton.isOver(pos):
                print("Login")
            if registerButton.isOver(pos):
                register()

        if event.type == pygame.MOUSEMOTION:
            if loginButton.isOver(pos):
                loginButton.colour = (0, 175, 0)
            else:
                loginButton.colour = (0, 255, 0)
            if registerButton.isOver(pos):
                registerButton.colour = (255, 0, 0)
            else:
                registerButton.colour = (175, 0, 0)


Here is the code I've used for the Registration Form:


from tkinter import *
tk = Tk()
tk.geometry('500x500')
tk.title("Registration Form")

Heading = Label(tk, text = "Registration Form", width = 20, font = ('bold', 20))
Heading.place(x = 120, y = 53)

hospitalName = Label(tk, text = "Hospital Name", width = 20, font = ('comicsans', 15))
hospitalName.place(x = 80, y = 130 )

entryBox_hosp = Entry(tk)
entryBox_hosp.place(x = 229, y = 130)

password = Label(tk, text = "Password", width = 20, font = ('comicsans', 15))
password.place(x = 75, y = 180)

entryBox_pass = Entry(tk)
entryBox_pass.place(x = 229, y = 180)

coordinates = Label(tk, text = "Coordinates", width = 20, font = ('comicsans', 15))
coordinates.place(x = 80, y = 230)

entryBox_crds = Entry(tk)
entryBox_crds.place(x = 229, y = 230)

def transfer():
    global hosp_name
    global password
    global coordinates
    hosp_name = entryBox_hosp.get()
    password = entryBox_pass.get()
    coordinates = entryBox_crds.get()

submit = Button(tk, text = "Submit", fg = 'red', width = 20, command = transfer)
submit.place(x = 150, y = 350)

mainloop()
For the Database:
import mysql.connector
import Registration_Form
mydb = mysql.connector.connect(
    host = #,
    user = #,
    passwd = #,
    database = #
)

myCursor = mydb.cursor()

sqlFormula = "INSERT INTO UserInfo (hosp_name, password, coordinates) VALUES (%s, %s, %s)"

hospital = [Registration_Form.hosp_name, Registration_Form.password, Registration_Form.coordinates]

myCursor.execute(sqlFormula, hospital)
mydb.commit()
NOTE: The database has already been created. These are just the steps of populating my database (The one which is being executed)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question log.exception() without arguments in old Python versions? cthart 5 1,116 Nov-19-2022, 07:09 PM
Last Post: Gribouillis
  what type of exception to use? Skaperen 3 997 Sep-30-2022, 05:00 PM
Last Post: Skaperen
  Exception handling in regex using python ShruthiLS 1 2,328 May-04-2020, 08:12 AM
Last Post: anbu23
  Exception: Returned Type Mismatch Error devansing 1 5,087 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  Type hinting - return type based on parameter micseydel 2 2,425 Jan-14-2020, 01:20 AM
Last Post: micseydel
  problem using custom exception handling in python srm 3 2,995 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  Stack trace shows different exception type than print micseydel 5 4,360 Apr-01-2019, 10:24 PM
Last Post: micseydel
  During handling of the above exception, another exception occurred Skaperen 7 26,723 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  Can't find error in code but Python throws exception Sandwich_masterX 3 2,861 Oct-09-2018, 01:38 AM
Last Post: ichabod801
  choice of exception type Skaperen 1 2,325 Sep-01-2018, 05:50 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020