More NullPointerExceptions
I am sure this comes up all the time too. I figured it had to do with my variable modifier since I'm calling methods from other classes, but I'm not quite sure. My error is: Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException
at Rosters.getSortTitle(Rosters.java:680)
at HubEvals.sort1RosterActionPerformed(HubEvals.java:219)
at HubEvals.access$400(HubEvals.java:17)
at HubEvals$5.actionPerformed(HubEvals.java:150)
Rosters.java:680
publicstaticvoid getSortTitle (int sort,int roster)
{
switch (roster)
{
case 1:
switch (sort)
{
case 1:
roster1Title.setText ("Daysort Roster");// line 680
break;
...
HubEvals.java:219
privatevoid sort1RosterActionPerformed (java.awt.event.ActionEvent evt)
{
Rosters.getSortTitle (sort1,1);// line 219
panelLayout ("roster1",sort1);
Rosters.setPositions (pos1,1);
Rosters.getEmployees (sort1,1);
}
HubEvals.java:17
publicclass HubEvalsextends javax.swing.JFrame//line 17
{
...
HubEvals.java:150
publicvoid actionPerformed(java.awt.event.ActionEvent evt)
{
sort1RosterActionPerformed(evt);// line 150
}
The only thing I can think of is my methods in the Rosters class requires variable values from HubEvals and in the code checking everything looks fine, but when I go to execute the method access to a variable isn't allowed and causing the exception, but I could be wrong. I tried setting all my variables that I will be passing topublic but it didn't seem to help.
I assume that roster1Title is a JLabel. Is this being properly initialized? (= JLabel()) before you are setting its text?
Yes, it's a JLabel. Actually, everything in the Rosters class was originally in the HubEvals class, but because I'm going to expand it more, I want to make these individual classes since I will be adding more features to it. I don't want all of my panels and methods in 1 class. I want them in seperate classes.
This all worked fine before I moved the stuff to the new class.
And yes. It's being initialized: private void initComponents()
{
rosterPanel1 = new javax.swing.JPanel();
roster1Title = new javax.swing.JLabel();
...
}
> Yes, it's a JLabel. ........
>
> This all worked fine before I moved the stuff to the
> new class.
but again, do you initialize the label before assigning to it?
........................................................ addendum..............
ah, I see.
Message was edited by:
petes1234
Do me a favor anyway. Please place this in your code:
switch (sort)
{
case 1:
System.out.println("Is roster1Title null?: " + (roster1Title == null));
//roster1Title.setText ("Daysort Roster"); // line 680
break;
Message was edited by:
petes1234
Well, it's says that it's true so it must be null.
I had its modifier set to public static sp I changed it back to private because the other class doesn't need to access it and now I get non-static variable roster1Title cannot be referenced from a static context
roster1Title.setText ("Daysort Roster");
Alright, so explain this to me, and I'm sure there is a valid reason behind it. In my HubEvals class, I was calling the method private void sort2RosterActionPerformed (java.awt.event.ActionEvent evt)
{
Rosters.getSortTitle (sort2,2);
panelLayout ("roster2",sort2);
Rosters.setPositions (pos2,2);
Rosters.getEmployees (sort2,2);
}
and that's when i was getting the non-static variable with static context error. By keeping my methods in the Rosters class non-static, I created an instance of the Rosters class in my methon in the HubEvals class: private void sort2RosterActionPerformed (java.awt.event.ActionEvent evt)
{
Rosters rosters = new Rosters();
rosters.getSortTitle (sort2,2);
panelLayout ("roster2",sort2);
rosters.setPositions (pos2,2);
rosters.getEmployees (sort2,2);
}
and it worked just fine. Why didn't I have to create an instance of the class? How is creating an instance different from calling the variable directly with the class reference?
You'll have to tap someone else's brain i'm afraid. the best I can do is prove that the jlabel was in fact not initiated at the point of exception. but for now, i'm long overdue for going to bed. good night and good luck!/Pete
I would recommend however that you get rid of as much static code and variables as possible. Sure, have static arround for library functions and such, but if you're programming w/ an OOP language, why not use it?again, good luck!
Well, it wasn't working. I mean I was getting rid of the error, but by creating an instance of the class in my functions and making nothing static only gave me more error. When I thought it was working, it was to an extent. That object was no longer null, but it wasn't setting the text like it should have.
I removed the lines in my Roster class of creating an instance of HubEvals (HubEvals hubEvals = new HubEvals(); because when I did that, I would login and a method would be called to create an instance of HubEvals and it was a continuous loop of creating an instance of that. I had 12 different frames opened and had to kill the processes.
Now I'm back to the original and I'm still getting the NullPointerException. Can you not setText() of a static variable? I thought it was only final variables that couldn't be changed.
> I removed the lines in my Roster class of creating an
> instance of HubEvals (HubEvals hubEvals = new
> HubEvals(); because when I did that, I would login
> and a method would be called to create an instance of
> HubEvals and it was a continuous loop of creating an
> instance of that. I had 12 different frames opened
> and had to kill the processes.
>
> Now I'm back to the original and I'm still getting
> the NullPointerException. Can you not setText() of a
> static variable? I thought it was only final
> variables that couldn't be changed.
you can set the text of a static variable, but being static or not may change its scope relative to instance methods and variables (more likely the other way around).
Well get this. I took everything back to how it originally was when I first started getting the error. My methods are still public static and my variables are public static as well. I commened out the lines to setText() of the label and added the line you told me use. I called the method once and got that it was true (null). Called it again and it returned false. After that I called the method for roster2Title and it was false both times...
Is roster1Title null?: true
Is roster1Title null?: false
Is roster2Title null?: false
Is roster2Title null?: false
Well I fixed that problem. I'm dumb. You were right. The component wasn't even getting initialized. Roster is just a JPanel form with 2 panels in it: roster1 and roster2. When I was trying to setText(), nothing had been called yet. In my HubEvals class (the main class), I added new Rosters();
and now I'm not getting the error at all. The only problem is the text won't change.
I don't know what happened but it started working...
I changed private void panelLayout (String panel, int rosterSort)
{
Rosters rosters = new Rosters();
mainPanel.add (rosters.rosterPanel1, "roster1");
mainPanel.add (rosters.rosterPanel2, "roster2");
CardLayout rosterLayout = (CardLayout)(mainPanel.getLayout ());
rosterLayout.show (mainPanel, panel);
}
to private void panelLayout (String panel, int rosterSort)
{
mainPanel.add (Rosters.rosterPanel1, "roster1");
mainPanel.add (Rosters.rosterPanel2, "roster2");
CardLayout rosterLayout = (CardLayout)(mainPanel.getLayout ());
rosterLayout.show (mainPanel, panel);
}
and now it works. All this trouble for something dumb.
Thanks again. Get some sleep now.