在java.awt中绘制时出现java.lang.NullPointerException

时间:2014-11-27 21:32:38

标签: graphics nullpointerexception awt

我想绘制一些球并与它们互动,但是当我运行我的程序时,我看到我的球保持静止,我得到java.lang.NullPointerException错误。你能告诉我我做错了什么吗?

主要课程:

public class Main extends JPanel {

// creating big ball
Big big = new Big(this);


// creating small balls
Small small_list = new Small(40);

// method for moving big ball
 private void moveBig() throws InterruptedException{
    big.move();
}
 // same but small balls
private void moveSmall() throws InterruptedException{
      // ERROR
    small_list.move();

}
 // paiting
@Override
    public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    big.paint(g2d);
    big.paintLine(g2d);

    small_list.paint(g2d);

    }

    public static void main(String[] args) throws InterruptedException {
    // TODO code application logic here

    JFrame window = new JFrame("test");
    Main main = new Main();

    window.add(main);
    window.setSize(500,500);
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while(true){
        main.moveBig();

        // ERROR
        main.moveSmall();

        main.repaint();
        Thread.sleep(15);
    }
}

}

大球课:

public class Big {

// coordinates of first apperence 
int x = 200;
int y = 200;

int upAnddown = 1;
int leftAndright = 1;
private static final int sizeOfbig = 30;
// list to draw a line after ball
private List<Point> listofpoints = new ArrayList<>();


private Main main;


public Big(Main main){

    this.main=main;

}



void move() throws InterruptedException{

    if (x+upAnddown < 5) {
        upAnddown=1;
    }
    if (x+upAnddown > main.getWidth()-14) {
        upAnddown=-1;
    }
    if (y+leftAndright < 5) {
        leftAndright=1;
    }
    if (y+leftAndright > main.getHeight()-14) {
        leftAndright=-1;
    }
    (...)

小球类:

public class Small {

int x = 20;
int y=20;

private static final int sizeOfSmall = 10;
int upAnddown = 1;
int leftAndright = 1;

private Main main;





public Small(Main main){
    this.main=main;

}

  public Small(int j){
  ArrayList<Small> my_array = new ArrayList<Small>(j);

      for (int i = 0; i < my_array.size(); i++) {
         i=j;
        my_array.add(new Small(i));
    }
}


void move() throws InterruptedException{


 //////////////////////////////////////    
    if (x+upAnddown < 0) {
        upAnddown=1;
    }
    // ERROR
    if (x+upAnddown > main.getWidth()-30) {
        upAnddown=-1;
    }
    if (y+leftAndright < 0) {
        leftAndright=1;
    }
    if (y+leftAndright > main.getHeight()-30) {
        leftAndright=-1;
    }
          (...)

ERROR:

Exception in thread "main" java.lang.NullPointerException
at test2.Small.move(Small.java:56)
at test2.Main.moveSmall(Main.java:34)
at test2.Main.main(Main.java:67)

1 个答案:

答案 0 :(得分:0)

while(true)将占用所有UI线程时间。你需要把它放在一个线程中以避免这种情况。

这就是为什么球保持不动。

new Thread(new Runnable(){

    public void run(){
        while(true) {
            main.moveBig();
            main.moveSmall();
            main.repaint();
            Thread.sleep(15);
        }

    }
}).Start();

NullPointerException可能与此问题有关,我不确定。我没有看到任何可能引起它的事。