• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing GUI w/ MySql Database  RSS feed

 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I've got another project for a course and am stuck. I've debugged and tried to figure out where it is breaking, but I just can't find it. I've used this connection code block as well as the contstructors before, but this just won't work. Could you guys give me some insight? I've got a tab that should send all of the information to a MySql database upon the click of 'Add Employee'. I've given my connection string, the addEmployee(); code, and if needed I can include the subclass code. I've got a superclass 'Employee' and a subclass 'Salaried' that uses four attributes from 'Employee'. Thanks for your help!



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what the problem is exactly: Does it compile? If not, then what is the error message that the compiler shows you? Do you get an error at runtime? What is the error? What exactly do you expect the program to do, and what does it really do, and how is that different from what you expected?

In some places you're catching an exception but you are not doing anything with the exception object itself, so if an exception happens the user won't know what the exact error is:

Try showing the exception message:

 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like it is stopping on line 11 of the first example above. It just hangs when I try to step through or over in debug mode. Where can I get info on why?
 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replied before seeing your reply, I'll add that in. It will not currently compile as I've added in a lot of functionality that is only partly developed. This add() feature is developed, so I was hoping to test it before moving onto the other items. One question I have, when I run the whole project it doesn't load my form. I've got my main class as EmployeeManager.java and then I created my swing form in EmployeeManagerApp.java, which is where all of this code is. So, to run the form, I've got to use Run File instead of Run Project. Should I remove the original EmployeeManager.java and make EmployeeManagerApp.java my only main class?

If I add ability to display the error on the catch, it seems like it never gets there. It is stuck on the setter for the variable socialSecurity. Here is the superclass if it helps:




Here is the error output:

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at employeemanager.EmployeeManagerApp.addEmployee(EmployeeManagerApp.java:302)
at employeemanager.EmployeeManagerApp.btnAddActionPerformed(EmployeeManagerApp.java:293)
...

You'll have to look at line number 302 of EmployeeManagerApp.java. Something is null there that is not supposed to be null.

You get a NullPointerException if you try to call a method or access a member variable via a variable that is null.
 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:

You'll have to look at line number 302 of EmployeeManagerApp.java. Something is null there that is not supposed to be null.

You get a NullPointerException if you try to call a method or access a member variable via a variable that is null.


Right, that is:



When I step through, 'socialSecurity' has a value that was entered in the text box. The line before, the variable 'socialSecurity' is correct, the only thing that happens between then and this line is the start of this line where it calls 'emp' which is declared at the top of the main class as:

 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The declaration for 'emp' is showing as null in my debug window. It's an overloaded constructor for Employee though, do I need to set it to something? I thought I could just declare it?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member variables are initialized to null by default. If you have just:

in your class, then emp will be null.
 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Member variables are initialized to null by default. If you have just:

in your class, then emp will be null.


This is really confusing me, because I've used a similar way of doing it in a past project and it worked fine. 'emp is just a new instance of the Employee class, so how should I be initializing it so it isn't null and can receive variables?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Rennard wrote:This is really confusing me, because I've used a similar way of doing it in a past project and it worked fine.

Then clearly it wasn't 'similar' enough, because what Jesper says is absolutely correct.

'emp is just a new instance of the Employee class, so how should I be initializing it so it isn't null and can receive variables?

Erm:
emp = new Employee();
or words to that effect? (possibly better done in a constructor too)

Winston

PS: Your code lines are far too long. I'd break them up myself, but you've got gazillions of them.
 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
James Rennard wrote:This is really confusing me, because I've used a similar way of doing it in a past project and it worked fine.

Then clearly it wasn't 'similar' enough, because what Jesper says is absolutely correct.

'emp is just a new instance of the Employee class, so how should I be initializing it so it isn't null and can receive variables?

Erm:
emp = new Employee();
or words to that effect? (possibly better done in a constructor too)

Winston

PS: Your code lines are far too long. I'd break them up myself, but you've got gazillions of them.


Thanks for more replies! I'll check out the link on long code lines. =)

As far as using - emp = new Employee(); - when I try that in my main class it tells me that Employee is abstract and cannot be instantiated. Employee is my superclass, so my plan was to use the constructor within Employee to start it off, then it would trigger the sublcass overloaded constructors in the subclass to finish up the statement. If you need any more information, please let me know. Thanks!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Rennard wrote:As far as using - emp = new Employee(); - when I try that in my main class it tells me that Employee is abstract and cannot be instantiated.

So presumably that's just one of the things that's different from when it "worked" before.

Employee is my superclass, so my plan was to use the constructor within Employee to start it off, then it would trigger the sublcass overloaded constructors in the subclass to finish up the statement.

Sounds to me like you're overthinking this by a country mile. If you have a concrete subclass of Employee, then use it:
emp = new Manager();
(or something like it) is still perfectly acceptable.

If you need any more information, please let me know. Thanks!

I think a more accurate description of the problem is what I need, because either I'm being very thick or I haven't a clue what you're trying to do.

Winston
 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No - you aren't thick, I'm just stuck and having to program based on a bunch of rules and regulations given by our course instructor. Essentially we have to use a super class 'Employee' with a constructor for the variables related to all employees. Then we have to have subclasses for 'Salaried', 'Manager', 'Director', and 'Hourly'. Those are all employees, having the same variables as 'Employee', but with their own in addition. So, here is my constructor for 'Employee' and this seems to be where it is getting stuck. When the button is clicked and it starts to use 'Employee', it just hangs:



When the button is clicked, it is trying to add to the employee database. When I originally used this type of constructor and even instantiated 'emp' using , it worked fine. So, when creating this application something must have changed in the constructor for Employee or in the main class. I get no errors or squigglies on the aforementioned declaration. In fact, 'emp' registers all the way through the main class, so it must be something in the Employee() class right?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're confused between declaring a variable and instantiating an object. That's a common confusion for Java beginners, but a variable is not an object. Instead, it holds (or rather, can hold) a reference to an object. So



does not instantiate any Employee objects. It only declares a variable which can refer to an Employee object. And when it's declared like that, its initial value is null, which means it right now isn't referring to any objects.

And you can't use it as if it did refer to an Employee object; if you do, you'll get the dreaded NullPointerException. So before you try to use it you have to assign a reference to an Employee object to the variable. I have no idea how you need to do that, I don't know how your application is supposed to work. If Employee is an abstract class then you'll have to assign a reference to a concrete subclass of Employee; again I have no idea where you might get that from.

And in answer to your other question: No, your NPE has absolutely nothing to do with how the Employee class is declared.
 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
And you can't use it as if it did refer to an Employee object; if you do, you'll get the dreaded NullPointerException. So before you try to use it you have to assign a reference to an Employee object to the variable. I have no idea how you need to do that, I don't know how your application is supposed to work. If Employee is an abstract class then you'll have to assign a reference to a concrete subclass of Employee; again I have no idea where you might get that from.

And in answer to your other question: No, your NPE has absolutely nothing to do with how the Employee class is declared.


I think this is where I am having issues. After digging into it more, I realized that in my other application I used a combo box to set a reference to 'emp'. Fortunately, I'm doing the same here, I have a combo box for 'Employee Type' and have set it to listen for changes to the box. I'll update you when I figure this part out. Thanks!

 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like I am good to go! I modified the combo box to create a reference to 'emp' when the employee type was selected. After working through some database connection issues, it is now connecting and I just added my first record! Thanks for your guys' help, couldn't have done it without you!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Rennard wrote:Thanks for your guys' help, couldn't have done it without you!

You're most welcome. Glad you got it solved.

Winston
 
James Rennard
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone is still around and checking this thread - would you mind helping me understand a requirement? They are asking that I override toString() for each subclass using super.toString(), but I cannot figure out what this means. In addition, I'm trying to display all records in my database to a text area and it is working, with the exception that it is printing the information in hex format. Please see my database call below and let me know your thoughts on overriding toString(). Thanks!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!