• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to add an instance of another Class from TextFields  RSS feed

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, I'm attempting to add an object's String to a TextArea in a Swing interface. I'm brand new to Swing as I'm familiar with JavaFX (don't know why my professor wants us to use Swing but oh well). For some reason, the String parsed from my variables is showing up as null, let's say for instance, I add a plant, it says (based off of my Plant class toString method, it gives the following String: "This plant's name is null with a color of: null with a unique ID of: null" Please let me know if further clarification is needed as I'm typing this hastily. I'm a bit desperate at this point.


GUI:



Plant Class:

 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your equals method is incorrect because you have not overridden hashCode in parallel.
Are you passing null to any constructors or setXXX methods? That is the only way you could get those three fields to point to null. I suggest you start by changing all the places where fields are assigned or reassigned to read
myField = Objects.requireNonNull(myParameter);
That shou‍ld soon tell you where you are getting the nulls.
In fact, I think you shou‍ld be protecting all reference type fields from having null assigned to them in all your classes like that.
I can see where you are getting values out of your text fields, but where are you using those values? Why are those three Strings fields rather than local variables? If there were local variables, you would find out about the problem soon enough because you would get a compiler error about not initialising them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!