当前位置: 动力学知识库 > 问答 > 编程问答 >

modifying a jlabel

问题描述:

I would like to modify a label. I create it at the top:

JLabel curStatus;

and then in a method that starts up all of the gui...

curStatus = new JLabel("");

jfrm.add(curStatus);

then for the method that takes a string, and puts it as the status of the jlabel:

public void setCurStatus(String inCurStatus) {

curStatus.setText("hi!"); <<< ERROR ON THIS LINE...

curStatus.setVisible(true);

}

I get this error:

Exception in thread "main" java.lang.NullPointerException

网友答案:

from the code snippet, it is clear that jLabel is null when you set the text. Obvious and only possible reason is : your GUI initialization code is not being executed before setting text. There cant be other reason at all.

You look again, and make sure that those initializations happen at the very first (call it in the top of constructor)

网友答案:

either remove the @param from the method or use

curStatus.setText(inCurStatus); // instead of curStatus.setText("Hi");

and pass "Hi" as an argument when you call the method.

分享给朋友:
您可能感兴趣的文章:
随机阅读: