java代码

时间:2015-12-17 21:42:44

标签: java nullpointerexception

我正在尝试使用以下代码制作一个“自由绘画”程序,如MS paint:

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

public class Liner extends JComponent{

static int MouseX1, MouseX2, MouseX3, MouseY1, MouseY2, MouseY3, MouseX4, MouseY4, choose, fNumber, lineThickness, size;
static Color colorChoice;
static ArrayList<sPoint> freeDraw = new ArrayList<sPoint>();
static ArrayList<Point> test = new ArrayList<Point>();
static JFrame window = new JFrame();
static Liner shapes = new Liner();

static JButton black = new JButton("black");
static JButton blue = new JButton("blue");
static JButton red = new JButton("red");

static JButton delete = new JButton("clear screen");
static JButton undo = new JButton("undo");

static JSlider thickness = new JSlider(1,50,5);

public static void main(String[] args){
    fNumber = 0;
    test.add(new Point(1,1));
    test.add(new Point(2,2));
    test.add(new Point(3,3));
    freeDraw.add(new sPoint(test,Color.BLACK,5));

    window.add(black);
    window.add(blue);
    window.add(red);
    window.add(delete);
    window.add(thickness);
    window.add(undo);

    black.setVisible(true);
    black.setSize(50,50);
    black.setLocation(0,0);
    black.addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e) {
            colorChoice = Color.BLACK;
        }
    });

    blue.setVisible(true);
    blue.setSize(50,50);
    blue.setLocation(0,50);
    blue.addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e) {
            colorChoice = Color.BLUE;;
        }
    });

    red.setVisible(true);
    red.setSize(50,50);
    red.setLocation(0,100);
    red.addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e) {
            colorChoice = Color.RED;
        }
    });

    delete.setVisible(true);
    delete.setSize(110,50);
    delete.setLocation(0,150);
    delete.addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e) {
            for(int i=0;i<=fNumber;i++){
                freeDraw.get(i).pointers.clear();
            }
            shapes.repaint();
        }
    });

    undo.setVisible(true);
    undo.setSize(50,50);
    undo.setLocation(0,200);
    undo.addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e) {
            freeDraw.trimToSize();
            size = freeDraw.size();
            freeDraw.get(size-1).pointers.clear();
            shapes.repaint();
        }
    });

    thickness.setVisible(true);
    thickness.setSize(100,20);
    thickness.setLocation(0,250);
    thickness.addChangeListener(new ChangeListener(){
        public void stateChanged(ChangeEvent e){
            lineThickness = thickness.getValue();   
        }
    });
    lineThickness = 2;

    shapes.addMouseListener(new MouseAdapter(){
        public void mousePressed(MouseEvent e) {
            freeDraw.add(new sPoint((new ArrayList<Point>()), colorChoice, lineThickness));    
            fNumber++;

        }
    });
   shapes.addMouseMotionListener(new MouseMotionAdapter(){
        public void mouseDragged(MouseEvent e){
            freeDraw.get(fNumber).pointers.add(new Point(e.getX(), e.getY()));
            shapes.repaint();
        }
    });    

    window.add(shapes); 
    window.setVisible(true);
    window.pack();
    window.setSize(1000,1000);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;   
        for(int a = 0;a<freeDraw.size();a++){
            g2.setStroke(new BasicStroke(freeDraw.get(a).thn));
            g.setColor(freeDraw.get(a).color);
            for(int b = 1;b< freeDraw.get(a).pointers.size();b++){ \\HERE is where the error is- ---- -- ------ -- ------- ---
                g.drawLine(freeDraw.get(a).pointers.get(b-1).x,freeDraw.get(a).pointers.get(b-1).y,freeDraw.get(a).pointers.get(b).x,freeDraw.get(a).pointers.get(b).y);
            }

        }

    }

}

这是“sPoint”类:

import java.util.ArrayList;

public class sPoint{
    ArrayList<Point> pointers;
    Color color;
    int thn;

public sPoint(ArrayList<Point> pointers, Color color, int thn){
}
}

我继续在行中嵌套for循环获得Null Pointer Exception;我在错误所在的代码中添加了注释。有什么建议?我试过创建一个“初始”sPoint对象,但这不能解决问题。我认为这与某个空阵列有关,但老实说我不知道​​。谢谢!

1 个答案:

答案 0 :(得分:0)

你有指针成员未初始化 - 你应该在 sPoint 类中初始化它然后 - 我想 - 在它的构造函数中进行赋值< / p>

ArrayList<Point> pointers = new ArrayList<Point>();

public sPoint(ArrayList<Point> pointers, Color color, int thn)
{
     this.pointers = pointers;
}