此问题已在此处找到答案:
什么是nullpointerexception,如何修复它((12个答案)
6天前关门。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JTextPane;
@SuppressWarnings({ "serial", "unused" })
public class jframe extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
jframe frame = new jframe();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public jframe() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel(" PRESS THE BUTTON TO GET CLICKS");
lblNewLabel.setFont(new Font("Sitka Small", Font.PLAIN, 15));
lblNewLabel.setBounds(29, 71, 382, 34);
contentPane.add(lblNewLabel);
JButton btnNewButton = new JButton("CLICK ME");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnNewButton.setForeground(Color.DARK_GRAY);
btnNewButton.setBackground(Color.CYAN);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myMethod();
}
});
btnNewButton.setBounds(153, 139, 116, 42);
contentPane.add(btnNewButton);
JTextPane txtpnClicks = new JTextPane();
txtpnClicks.setForeground(Color.GRAY);
txtpnClicks.setText(" CLICKS:"+x);
txtpnClicks.setFont(new Font("Sylfaen", Font.PLAIN, 15));
txtpnClicks.setBounds(86, 189, 275, 28);
contentPane.add(txtpnClicks);
}
static int x = 1;
@SuppressWarnings("null")
static void add1() {
x++;
textpnClicks.setText("CLICKS"+x);
}
}
I have tried looking for the answer online. When I run the code this error comes up in the console :
线程“awt-eventqueue-0”java.lang.nullpointerexception中的异常:无法调用“javax.swing.jlabel.settext(string)”,因为jframe$2处的jframe.mymethod(jframe.java:77)处的“txtpnclicks”为空。java.desktop/javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:1967)处的actionperformed(jframe.java:59)为空java.desktop/javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2308)位于java.desktop/javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:405)位于位于java.desktop/java.awt.component.processmouseevent(component.java:6379)的java.desktop/java.awt.component.processmouseevent(component.java:6614)的java.desktop/javax.swing.jcomponent.processmouseevent(jcomponent.java:3342)的java.desktop/java.awt.component.component.processevent(component.java:6379)的javajava.desktop/java.awt.container.processevent(container.java:2263)在java.desktop/java.awt.component.dispatcheventimpl(component.java:4990)在java.desktop/java.awt.container.dispatcheventimpl(container.java:2321)在java.desktop/java.awt.component.dispatchevent(component.java:4822)在java.desktop/java.awt.lightweightdispatcher.RetargetMouseeEvent(container.java:4919)在java.desktop/java.awt.lightweightdispatcher.ProcessMouseeEvent(container.java:4548)在java.desktop/java.awt.container.DispatcheEventImpl(container.java:2307)在java.desktop/java.awt.window.dispatcheventimpl(window.java:2769)在java.desktop/java.awt.component.dispatchevent(component.java:4822)在java.desktop/java.awt.eventqueue.dispatcheventimpl(eventqueue.java:772)在java.desktop/java.awt.eventqueue$4.run(eventqueue.java:721)在java.desktop/java.awt.awt.eventqueue$4.run(eventqueue.java:715)在java.base/java.security.accesscontroller.doprivileged(accesscontroller.java:391)位于java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:85)位于java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:95)位于java.desktop/java.awt.eventqueue$5.run(eventqueue.java:745)在java.desktop/java.awt.eventqueue$5.run(eventqueue.java:743)在java.base/java.security.accesscontroller.doprivileged(accesscontroller.java:391)在java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protection.java:85)在位于java.desktop/java.awt.eventqueue.dispatchevent(eventqueue.java:742)的java.desktop/java.awt.eventdispatchthread.PumponeeEventForFilters(eventdispatchthread.java:203)的java.desktop/java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:124)位于java.desktop/java.awt.eventdispatchthread.PumpeEventsforHierarchy(eventdispatchthread.java:113)位于java.desktop/java.awt.eventdispatchthread.PumpeEvents(eventdispatchthread.java:109)位于java.desktop/java.awt.eventdispatchthread.run(eventdispatchthread.java:101)位于java.desktop/java.awt.java:90线程“awt-eventqueue-0”java.lang.nullpointerexception中的异常:无法调用“javax.swing.jlabel.settext(string)”,因为jframe$2处的jframe.mymethod(jframe.java:79)处的“txtpnclicks”为空。java.desktop/javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:1967)处的actionperformed(jframe.java:60)为空java.desktop/javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2308)位于java.desktop/javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:405)位于位于java.desktop/java.awt.component.processmouseevent(component.java:6379)的java.desktop/java.awt.component.processmouseevent(component.java:6614)的java.desktop/javax.swing.jcomponent.processmouseevent(jcomponent.java:3342)的java.desktop/java.awt.component.component.processevent(component.java:6379)的javajava.desktop/java.awt.container.processevent(container.java:2263)在java.desktop/java.awt.component.dispatcheventimpl(component.java:4990)在java.desktop/java.awt.container.dispatcheventimpl(container.java:2321)在java.desktop/java.awt.component.dispatchevent(component.java:4822)在java.desktop/java.awt.lightweightdispatcher.RetargetMouseeEvent(container.java:4919)在java.desktop/java.awt.lightweightdispatcher.ProcessMouseeEvent(container.java:4548)在java.desktop/java.awt.container.DispatcheEventImpl(container.java:2307)在java.desktop/java.awt.window.dispatcheventimpl(window.java:2769)在java.desktop/java.awt.component.dispatchevent(component.java:4822)在java.desktop/java.awt.eventqueue.dispatcheventimpl(eventqueue.java:772)在java.desktop/java.awt.eventqueue$4.run(eventqueue.java:721)在java.desktop/java.awt.awt.eventqueue$4.run(eventqueue.java:715)在java.base/java.security.accesscontroller.doprivileged(accesscontroller.java:391)位于java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:85)位于java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:95)位于java.desktop/java.awt.eventqueue$5.run(eventqueue.java:745)在java.desktop/java.awt.eventqueue$5.run(eventqueue.java:743)在java.base/java.security.accesscontroller.doprivileged(accesscontroller.java:391)在java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protection.java:85)在位于java.desktop/java.awt.eventqueue.dispatchevent(eventqueue.java:742)的java.desktop/java.awt.eventdispatchthread.PumponeeEventForFilters(eventdispatchthread.java:203)的java.desktop/java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:124)位于java.desktop/java.awt.eventdispatchthread.PumpeEventsforHierarchy(eventdispatchthread.java:113)位于java.desktop/java.awt.eventdispatchthread.PumpeEvents(eventdispatchthread.java:109)位于java.desktop/java.awt.eventdispatchthread.run(eventdispatchthread.java:101)位于java.desktop/java.awt.java:90
1条答案
按热度按时间s5a0g9ez1#
功能
add1()
不知道任何关于JTextPane
类别-txtpnClicks
,因此无法调用方法settext()。错误日志中写得很清楚线程“awt-eventqueue-0”java.lang.nullpointerexception中的异常:无法调用“javax.swing.jlabel.settext(string)”,因为jframe.mymethod(jframe.java:77)中的“txtpnclicks”为空
您需要将其传递给函数
此外
txtpnClicks
必须在之前创建btnNewButton
,否则btnNewButton.addActionListener(e -> add1(txtpnClicks));
我不明白是什么txtpnClicks
工作代码: