[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:Iwan,
That's a lot of code! Can you narrow it down to create a Small Self Contained Example? This let you get rid of the classes that aren't causing problems. Also, I don't see a main method so I'm not clear on how you run the code or what line the error occurs on.
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
All things are lawful, but not all things are profitable.
Knute Snortum wrote:You say the code is "not working." Could you be more specific?
All things are lawful, but not all things are profitable.
Knute Snortum wrote:Can you post the full message of the error, including the stack trace?
All things are lawful, but not all things are profitable.
Knute Snortum wrote:The important part of the stack trace for you is here:
studentadmingui.StudentAdminFrame.studentButtonAction(StudentAdminFrame.java:75)
It say that the error occurred on line 75 of a class called StudentAdminFrame in a method called studentButtonAction. Could you post this class or at least the method?
All things are lawful, but not all things are profitable.
Knute Snortum wrote:So something on this line can throw a NullPointerException (NPE). Do you know what it is?
All things are lawful, but not all things are profitable.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
I don't think that's necessary, and it could throw a NPE.
Knute Snortum wrote:
I don't think that's necessary, and it could throw a NPE.
Knute Snortum wrote:It looks like studentTextField can be null. Maybe you didn't call getStudentTextField() before studentButtonAction()?
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
Knute Snortum wrote:It looks like studentTextField can be null. Maybe you didn't call getStudentTextField() before studentButtonAction()?
I still think this is the problem.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:I think studentTextField is null.
Campbell Ritchie wrote:I have come late to this discussion after Knute has done all the work.
Don't initialise fields to null. Don't rely on methods to test for nullity; obviously you are trying to use that field before it is given a real value. Remove the == null test from all methods. Write a constructor for your class and initialise all fields, including that text component, to real values.
Remember field means a piece of information contained in the class or object, not a kind of text component.
Carey Brown wrote:You might want to try
Everything.Iwan van den Hoek wrote:. . . what is wrong with this:
. . .
All things are lawful, but not all things are profitable.
Campbell Ritchie wrote:
Everything.Iwan van den Hoek wrote:. . . what is wrong with this:
. . .
You are confusing the field and a local variable. You are doing something in a method which ought to be done in the constructor. You are using setBounds which suggests you are not using a layout correctly. The only thing which would make that method worse would be to give it public access.
The method with the for‑each loop is an incorrect use of a List. In fact it suggests to me you do not want a List at all, but a different data structure.
Carey Brown wrote:I think I didn't take it far enough. Try
In your original code (above) for this, if you are getting a NPE on this line, it can only be one of:
admin studentTextField opleidingComboBox
Insert three print statements for these variables just before line 75 to see which one is null.
I will notify the Teacher that his code is wrong.
I don't want a different data structure.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
I will notify the Teacher that his code is wrong.
Okay, maybe don't do that, but be aware that there are better ways to code this.
I don't want a different data structure.
Well okay, but what you are describing would really lend itself to a Map of some kind. Your "loop through the students" use of a List will work fine, though (if more slowly).
All things are lawful, but not all things are profitable.
Knute Snortum wrote:Well, what's not happening? I take it that the student isn't getting added to the students array. If so, then when I look at this code...
... I see two things. If opleiding isn't an instance of Opleiding or richting doesn't equal the NaamStudie.
So why does opleiding have to be an instance of Opleiding? From a syntactical point of view, it doesn't. Being a Studie is good enough. Is there a logical reason? Using instanceof is often a red flag that the design is poor.
Next, you pass in richting and test it against NaamStudie. What stops there from being no name equal to richting? Or several names? You might want to use an enum for this rather than a String.
So again, to debug, use System.out.println (SOP) to print whether opleiding is an instance of Opleiding and if richting equals the NaamStudie. But keep in mind that there are better ways to accomplish what you're trying to do.
Knute Snortum wrote:Well, what's not happening? I take it that the student isn't getting added to the students array. If so, then when I look at this code...
... I see two things. If opleiding isn't an instance of Opleiding or richting doesn't equal the NaamStudie.
So why does opleiding have to be an instance of Opleiding? From a syntactical point of view, it doesn't. Being a Studie is good enough. Is there a logical reason? Using instanceof is often a red flag that the design is poor.
Next, you pass in richting and test it against NaamStudie. What stops there from being no name equal to richting? Or several names? You might want to use an enum for this rather than a String.
So again, to debug, use System.out.println (SOP) to print whether opleiding is an instance of Opleiding and if richting equals the NaamStudie. But keep in mind that there are better ways to accomplish what you're trying to do.
Liutauras Vilda wrote:Iwan van den Hoek, I am looking only at your latest post and have a feeling, that you didn't decompose your method enough. In effect of that, your method does something different than its name suggests. And most likely it does more than it should, so your confusion is absolutely understandable.
Could you please explain in english, what this method suppose to be doing?
Not sure understood that. Is regular student the one who enrolled to a full study programme, and the other type is the one who only enrolled to a one particular module to study?Iwan van den Hoek wrote:A student can be a RegulierStudent with a study or a scholer with a course, but not both.
Iwan van den Hoek wrote:... I want to create a new Student with the selected study...
Iwan van den Hoek wrote:In Student administration I have 2 Arraylists one for students and one for Study's and courses. A student can be a RegulierStudent with a study or a scholer with a course, but not both.
I have to add a student based on the textfield in the Gui to the studenten ArrayList and check the already filled opleidingen Arraylist for the Study based on the selected String value in the pulldown menu from the Jcombobox.
if the chosen String value corresponds with a Value in the opleidingen ArrayList I want to add the student with that particular study/course to the studenten Arraylist.
All things are lawful, but not all things are profitable.
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|