【问题标题】:java.lang.NullPointerException while drawing in java.awt在 java.awt 中绘图时出现 java.lang.NullPointerException
【发布时间】:2015-01-26 12:53:28
【问题描述】:

我想画一些球并与它们互动,但是当我运行我的程序时,我看到我的球静止不动,并且出现 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;
    }
          (...)

错误:

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)

【问题讨论】:

    标签: graphics nullpointerexception awt


    【解决方案1】:

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

    这就是球保持静止的原因。

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

    NullPointerException 可能与这个问题有关,我不确定。我看不出还有什么可以提高它的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      相关资源
      最近更新 更多