2

Problem With NPE that shouldn't be Null at all, Plz help

Hi guys, i have this code which displays an object in a table the method savet saves the object into that class so it can be referenced, and it works(tried and tested) but when i try to call method displayTeacher i get a null pointer exception. this is because it is not recognizing t1, even though it is clearly in the class,Even as an attribute

public class Display
{
    private JTable table;
    private JFrame f;
    private int i = 0;
    public SubjectTeacher t1;

    public void savet (SubjectTeacher teachIn)
    {
        SubjectTeacher tempt = new SubjectTeacher(teachIn.getName(), teachIn.getSurname(), teachIn.getID(), teachIn.getPay(), teachIn.getSubjectID());
        t1 = tempt;
    }
    
    public void displayTeachers()
    {
        f = new JFrame("Teachers");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500,400);
        f.setVisible(true);
    
        String[] columnNames =  {"Name", "Surname", "ID", "Pay", "Subject"};
        Object[][] data = {
            {
             t1.getName(),
             t1.getSurname(),
             t1.getID(),
             t1.getPay(),
             t1.getSubjectID()
            }
          };
    JTable table = new JTable (data, columnNames);
    f.add(table);
    }
}

Any help is greatly appreciated since i must finish a school project very soon, Thanks,Matt

6
  • 6
    do you call public void savet (SubjectTeacher teachIn) method before? t1 is null until it's assigned to an object. Commented Nov 22, 2012 at 16:18
  • Why not just create the SubjectTeacher object in the constructor and assign it to t1 instead of assigning tempt to t1? Commented Nov 22, 2012 at 16:19
  • show the flow control of your code .. Commented Nov 22, 2012 at 16:21
  • It would really help if you provide information about the location (exact line) where the NPE occurs... Now we have to guess, which makes it much harder to help you out. (Note: a line number does not help, as you example snippet does not have line numbers) Commented Nov 22, 2012 at 16:22
  • sorry about that, it comes up on `` public void displayTeachers() { f = new JFrame("Teachers"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(500,400); f.setVisible(true); String[] columnNames = {"Name", "Surname", "ID", "Pay", "Subject"}; Object[][] data = { { t1.getName(), t1.getSurname(), t1.getID(), t1.getPay(), t1.getSubjectID() } };`` that part of the code, in the first reference to t1 @Veger Commented Nov 22, 2012 at 17:09

5 Answers 5

2

this is because it is not recognizing t1, even though it is clearly in the class

It's in the class, but is it ever initialized? Reference type fields have a default value of null, so if you never assign t1 to anything before trying to call non-static methods on it you will get a NullPointerException (since t1 would be null). In your case, the only place you are assigning t1 is in the savet method, so if you don't call this method before you call displayTeachers t1 will still be null.

Sign up to request clarification or add additional context in comments.

3 Comments

the problem is i have tried it and savet works, i always call savet before displayTeachers, and so t1 will be populated...
@MatthewCassar Try printing t1 at the start of displayTeachers. Also, is there any other place in the code where you assign t1?
i have done that already the line System.out.println(t1.getName()); works in savet but not when called immediately after in displayTeachers
1

You said that you call the 2 methods from different classes. Are you sure you call the methods on the same instance of Display? You probably need only one instance of Display, so you should use Singleton Design Pattern, to ensure that there is only one instance.

Another observation is that in you method:

        public void savet (SubjectTeacher teachIn)
            {
                SubjectTeacher tempt = new SubjectTeacher(teachIn.getName(), teachIn.getSurname(), teachIn.getID(), teachIn.getPay(), teachIn.getSubjectID());
                t1 = tempt;
            }

it would be easier if you write t1=teachIn, and would have the same effect.

EDIT: Your Display class should look something like this:

    import javax.swing.JFrame;
    import javax.swing.JTable;

    public class Display {
        private static Display instance;
        private JFrame f;
        public SubjectTeacher t1;

        private Display() {
        }

        public static Display getInstance() {
            if (instance == null) {
                instance = new Display();
            }

            return instance;
        }

        public void savet(SubjectTeacher teachIn) {
            t1 = teachIn;
        }

        public void displayTeachers() {
            f = new JFrame("Teachers");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(500, 400);
            f.setVisible(true);

            String[] columnNames = { "Name", "Surname", "ID", "Pay", "Subject" };
            Object[][] data = { { t1.getName(), t1.getSurname(), t1.getID(),
                    t1.getPay(), t1.getSubjectID() } };

            f.add(new JTable(data, columnNames));
        }
    }

When you want to create a new instance of display: instead of Display d = new Display(); you should type: Display d = Display.getInstance(); This way, there will be only one instance of Display, and you will be referring to the same object in both classes.

4 Comments

you call the methods on the same instance of Display?
i cant use the same instance, since they are in 2 different classes in menu class d.displayTeachers(); in enter teachers class dp.savet(subTeach[iT]);
Well I think there is your problem. You can either make the Display class`s methods and atributes static (all the methods and atributes you call from other classes should be static), and make no instance of the class, and call the methods with Display.method(); and refer the atributes with Display.atribute; Or you can use Singleton Design Pattern.
this is starting to seem impossible
0

Is savet called from a different thread then displayTeachers? If so try replacing public SubjectTeacher t1; with public volatile SubjectTeacher t1;

1 Comment

i am calling the 2 methods from different classes, however volatile still doesnt work :/ thans anyway
0

Make sure you are calling savet(SubjectTeacher) method before you call displayTeachers(). My guess is that you are calling displayTeachers() and the savet(SubjectTeacher) has not been called. So t1 is null and you are getting the NullPointerException when you try to get the data from t1.

1 Comment

I am calling that, (there is a whole other class for entering a teachers details) and t1, when tested in savet works
0

You mention that you use the Display class in two other classes.

Do you have 2 different instantiations? Or in other terms: do you call new Display() in both classes?

If so, are you 100% sure that in both classes you call the savet() method?

It might be possible that you call savet() for one instantiation and displayTeachers() for the other instantiation. Which will definitally result in your NPE problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.