简体   繁体   English

Java:简单GUI程序的NullPointerException

[英]Java: NullPointerException of a simple GUI program

Hi I am novice Java learner. 嗨,我是Java新手。 I was tring to implement a simple GUI program to change the color of a panel on clicking clicking of a button. 我正试图实现一个简单的GUI程序,以在单击按钮时单击以更改面板的颜色。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Button_lable implements ActionListener {
public JFrame frame;
//JPanel panel;
//JLabel label;

public static void main(String[] args) {

    Button_lable gui = new Button_lable();
    gui.go();
}//end of main
public void go(){
    //System.out.println("Entered Go()");
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton b_frame = new JButton("Click to change the color");
    b_frame.addActionListener(this);
    MyDrawpanel d_panel = new MyDrawpanel();

    frame.getContentPane().add(BorderLayout.SOUTH,b_frame);
    frame.getContentPane().add(BorderLayout.CENTER ,d_panel);

    frame.setSize(300, 300);
    frame.setVisible(true);


}//end of go
public void actionPerformed(ActionEvent e) {
    frame.repaint();
}


}//end of Button_lable


class MyDrawpanel extends JPanel {
    public void paintComponent(Graphics g){
Graphics2D grph = (Graphics2D) g;
int red = (int)(Math.random()* 255);
int green = (int)(Math.random()* 255);
int blue = (int)(Math.random()* 255);
Color strt_clr = new Color(red,green,blue);

red = (int)(Math.random()* 255);
green = (int)(Math.random()* 255);
blue = (int)(Math.random()* 255);
Color end_clr = new Color(red,green,blue);

GradientPaint gradient = new GradientPaint(70,70,strt_clr,150,150,end_clr);  
grph.setPaint(gradient);
grph.fillOval(50,25, 150, 150);
  }
}

I get output window. 我得到输出窗口。 But when i click on the button, i get the following exception. 但是当我单击按钮时,出现以下异常。

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Button_lable.actionPerformed(Button_lable.java:34)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Please advice. 请指教。

Kind Regards. 亲切的问候。

You never initialize this.frame , so it is null . 您永远不会初始化this.frame ,因此它为null

In go() , you create and initialize a different variable called frame : go() ,创建并初始化一个不同的变量,称为frame

JFrame frame = new JFrame();

You might want to remove the first JFrame : 您可能要删除第一个JFrame

frame = new JFrame();

You have declared 2 variables called frame - one class variable and one inside go . 您已经声明了2个称为frame变量-一个是类变量,另一个是在go Remove the declaration inside go & just initialize the class variable inside go 删除go内的声明并仅初始化go内的类变量

public void go(){
    //System.out.println("Entered Go()");
    JFrame frame = new JFrame();

Change the above to 将以上更改为

public void go(){
    //System.out.println("Entered Go()");
    frame = new JFrame();

Because you are creating a new variable called frame in go , the class variable frame never gets initialized. 因为要在go中创建一个称为frame的新变量,所以永远不会初始化类变量frame It remains null and hence the NullPointerException 它仍然为null ,因此为NullPointerException

frame is null. 框架为空。 Just use a setter for frame. 只需使用二传手为框架。 Something like : 就像是 :

void setFrame(JFrame theFrame) {
    this.frame = theFrame;
}

Also you have declared frame twice. 另外,您已两次声明帧。 Remove it from go(); 从go()中删除它;

Change this line: 更改此行:

JFrame frame = new JFrame();

by this: 这样:

frame = new JFrame();
public class Button_lable implements ActionListener {
public JFrame frame; // This is a class/global variable
//JPanel panel;
//JLabel label;

public static void main(String[] args) {

    Button_lable gui = new Button_lable();
    gui.go();
}//end of main
public void go(){
    //System.out.println("Entered Go()");
    JFrame frame = new JFrame();  // This is local variable.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton b_frame = new JButton("Click to change the color");
    b_frame.addActionListener(this);
    MyDrawpanel d_panel = new MyDrawpanel();

    frame.getContentPane().add(BorderLayout.SOUTH,b_frame);
    frame.getContentPane().add(BorderLayout.CENTER ,d_panel);

    frame.setSize(300, 300);
    frame.setVisible(true);


}

You have defined class variable as frame. 您已将类变量定义为框架。 You are instantiating local variable frame. 您正在实例化局部变量框架。 So without assigning it is very obvious that it will throw NPE. 因此,如果不进行分配,很明显它将抛出NPE。 Because the scope of local variable resides in particular method only. 因为局部变量的范围仅驻留在特定方法中。

You just need to do following. 您只需要执行以下操作。

public class Button_lable implements ActionListener {
public JFrame frame;
//JPanel panel;
//JLabel label;

public static void main(String[] args) {

    Button_lable gui = new Button_lable();
    gui.go();
}//end of main
public void go(){
    //System.out.println("Entered Go()");
    frame = new JFrame(); // here, the class variable is instantiated. So it wont give NPE.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton b_frame = new JButton("Click to change the color");
    b_frame.addActionListener(this);
    MyDrawpanel d_panel = new MyDrawpanel();

    frame.getContentPane().add(BorderLayout.SOUTH,b_frame);
    frame.getContentPane().add(BorderLayout.CENTER ,d_panel);

    frame.setSize(300, 300);
    frame.setVisible(true);


}

Let me explain the reason of Exception. 让我解释一下异常的原因。

the first public JFrame frame; 第一个public JFrame frame; you declare under first braces has global scope. 您声明第一个花括号具有全局范围。 And the second JFrame frame = new JFrame(); 第二个JFrame frame = new JFrame(); you initialize creates a new instance of it. 您初始化会为其创建一个新实例。 the frame you declare in go() method has local scope and it initialize it not the global one. 您在go()方法中声明的框架具有局部范围,并且不会将其初始化为全局范围。 That's why global scope instance of frame is uninitialized . 这就是为什么global scope instance of frame is uninitialized it is causing NullPointerException . 它导致NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM