gpt4 book ai didi

java - 我的类(class)出现空指针错误

转载 作者:行者123 更新时间:2023-12-01 18:35:47 25 4
gpt4 key购买 nike

我的教授给了我这段代码,它应该按原样运行。我编译它并收到以下错误。

java.lang.NullPointerException
at WholePanel.<init>(WholePanel.java:59)
at Assignment7.init(Assignment7.java:19)
at sun.applet.AppletPanel.run(AppletPanel.java:435)
at java.lang.Thread.run(Thread.java:744)

我不知道为什么会发生这种情况。这是我的类(class)。我编译并运行了代码,得到了错误,尝试注释掉一些东西,但仍然没有任何结果。我已经编写了一些以前的小程序,它们运行良好。

作业7

<小时/>
import javax.swing.*;

public class Assignment7 extends JApplet
{

public void init()
{
// create a WholePanel object and add it to the applet
WholePanel wholePanel = new WholePanel();
getContentPane().add(wholePanel);

//set applet size to 400 X 400
setSize (400, 400);
}

}

整个面板

<小时/>
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;

public class WholePanel extends JPanel
{
private Color currentColor;
private CanvasPanel canvas;
private JPanel primary, buttonPanel, leftPanel;
private JButton erase, undo;
private ArrayList rectList, tempList;
private JRadioButton[] colorRButtons;
private Color[] colors;
private int x1, y1, x2, y2, x3, y3;
private boolean mouseDragged = false;

//Constructor to instantiate components
public WholePanel()
{
//default color to draw rectangles is black
currentColor = Color.black;
rectList = new ArrayList();

//create buttons






//create radio buttons for 5 colors
//black will be chosen by default
colorRButtons = new JRadioButton[5];
colorRButtons[0] = new JRadioButton("black", true);


//store 5 colors in an array


//group radio buttons so that when one is selected,
//others will be unselected.
ButtonGroup group = new ButtonGroup();
for (int i=0; i<colorRButtons.length; i++)
group.add(colorRButtons[i]);

//add ColorListener to radio buttons
ColorListener listener = new ColorListener();
for (int i=0; i<colorRButtons.length; i++)
colorRButtons[i].addActionListener(listener);

//primary panel contains all radiobuttons
primary = new JPanel(new GridLayout(5,1));
for (int i=0; i<colorRButtons.length; i++)
primary.add(colorRButtons[i]);


//canvas panel is where rectangles will be drawn, thus
//it will be listening to a mouse.
canvas = new CanvasPanel();
canvas.setBackground(Color.white);
canvas.addMouseListener(new PointListener());
canvas.addMouseMotionListener(new PointListener());

JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, canvas);

setLayout(new BorderLayout());
add(sp);
}

//ButtonListener defined actions to take in case "Create",
//"Undo", or "Erase" is chosed.
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{










}
} // end of ButtonListener

// listener class to set the color chosen by a user using
// the radio buttons.
private class ColorListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == colorRButtons[0])
currentColor = colors[0];
else if (event.getSource() == colorRButtons[1])
currentColor = colors[1];
else if (event.getSource() == colorRButtons[2])
currentColor = colors[2];
else if (event.getSource() == colorRButtons[3])
currentColor = colors[3];
else if (event.getSource() == colorRButtons[4])
currentColor = colors[4];
}
}


//CanvasPanel is the panel where rectangles will be drawn
private class CanvasPanel extends JPanel
{
//this method draws all rectangles specified by a user
public void paintComponent(Graphics page)
{
super.paintComponent(page);

//draw all rectangles
for (int i=0; i < rectList.size(); i++)
{
// ((Rect) rectList.get(i)).draw(page);
}

//draw an outline of the rectangle that is currently being drawn.
if (mouseDragged == true)
{
page.setColor(currentColor);
//Assume that a user will move a mouse only to left and down from
//the first point that was pushed.
page.drawRect(x1, y1, x3-x1, y3-y1);
}

}
} //end of CanvasPanel class

// listener class that listens to the mouse
public class PointListener implements MouseListener, MouseMotionListener
{
//in case that a user presses using a mouse,
//record the point where it was pressed.
public void mousePressed (MouseEvent event)
{
//after "create" button is pushed.





}

//mouseReleased method takes the point where a mouse is released,
//using the point and the pressed point to create a rectangle,
//add it to the ArrayList "rectList", and call paintComponent method.
public void mouseReleased (MouseEvent event)
{




}

//mouseDragged method takes the point where a mouse is dragged
//and call paintComponent nethod
public void mouseDragged(MouseEvent event)
{



canvas.repaint();
}

public void mouseClicked (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
public void mouseMoved(MouseEvent event) {}

} // end of PointListener

} // end of Whole Panel Class

最佳答案

您似乎只创建了一种 colorRButtons,但试图使用所有五个。

所以在 colorRButtons[0] 中不为 null,但所有其他都为 null,并且对它们使用 addActionListener 是不可能的。

关于java - 我的类(class)出现空指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22089205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com