NullPointerException when referencing a property of a JTabbedPane
What is wrong with the logic behind creating a JTabbedPane
and getting the tab count
of 11, then using a setter?
I try to reference the tabCount
of the SetObjects.currentJTabbedPane
and it throws
NullPointerException
.
create
jtp.setSelectedIndex(0);
int jtpCount=jtp.getTabCount();
System.out.println(C+M+AND+jtpCount+": jtpCount of The JTabbedPane created: \n");
setCurrentJTabbedPane(jtp);
int currentCount=SetO开发者_运维百科bjects.currentJTabbedPane.getTabCount();
System.out.println(C+M+AND+currentCount+": currentCount for
SetObjects.currentJTabbedPane.getTabCount() : \n");
setter
public static void setCurrentJTabbedPane(JTabbedPane currentJTabbedPane) {
String M =(" --> in setCurrentJTabbedPane() var: <-- \n");
System.out.println(M);
SetObjects.currentJTabbedPane = currentJTabbedPane;
System.out.println(C+M+AND+SetObjects.currentJTabbedPane.getTabCount()+":
SetObjects.currentJTabbedPane.getTabCount() : \n");
}
output
CLASS
SetObjects: --> JTabbedPane obj() var: xxxxxxxxx : xxxxxxxx<-- : 11: jtpCount of The JTabbedPane created:--> in setCurrentJTabbedPane() var: <--
Exception in thread "main" java.lang.NullPointerException
at view.SetObjects.setCurrentJTabbedPane(SetObjects.java:385) at view.SetObjects.obj(SetObjects.java:217) at view.SchoolJDesktopPane.createInnerFrame(SchoolJDesktopPane.java:119) at view.SchoolJDesktopPane.(SchoolJDesktopPane.java:97) at view.Main.createJDesktopPane(Main.java:60) at view.Main.main(Main.java:54)
Are you passing null
to setCurrentJTabbedPane
? Try adding more System.out.println statements to inspect each variable that might be the problem. Also, make your debugging statements simpler, i.e. replace System.out.println(A + B + C)
with three different statements.
EDIT: The reason for splitting up debug statements is that if System.out.println(object1.foo() + ", " + object2.bar())
throws a NullPointerException then you don't know which object was null. Splitting it into two statements means the stack trace tells you exactly where the problem was.
精彩评论