为什么这个Java代码不起作用?

时间:2015-03-03 18:16:03

标签: java json error-handling

我制作了一个程序,由于某种原因,它无法正常工作。 (问题出在Keylistener) 我在互联网上搜索了一个问题,我找到了一个代码(它是可复制的),这与我的相似。 有了它的帮助,我成功地制作了一个有效的代码,但之后,当我开始改变它时,使其与帮助代码更加相似(因为它是一个很好的解决方案)。 现在程序启动了,但它抛出了很多错误信息。

问题是:为什么第一个代码不工作,第二个代码工作时,第一个代码的解决方案是什么?

第一个(我的)代码:

Menu.java:

class Menu {
    public static void main(String[] args) {
        start go = new start();
        go.main();
    }
}

class start {

    void main() {
        Mozgas rajta = new Mozgas();
        rajta.menet();
    }

}

Mozgas.java:

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

class Mozgas
extends Applet
implements KeyListener, ActionListener{

    JFrame keret;
    Palya field;
    int irany;
    static int x = 32;
    static int y = 32;
    int xv, yv = 0;
    Timer t = new Timer(50, this);

    void menet() {

        keret = new JFrame("Snake");
        field = new Palya();

        Mozgas m = new Mozgas();
        keret.add(m);


        keret.getContentPane().add(BorderLayout.CENTER, field);
        keret.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        keret.setSize(1200,678);
        keret.setVisible(true);


    }


    public Mozgas () {
        t.start();

        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }









    @Override
    public void actionPerformed(ActionEvent e) {
        Mozgas.setx(Mozgas.getx()+xv*32);
        Mozgas.sety(Mozgas.gety()+yv*32);
        field.repaint();
    }


    @Override
    public void keyPressed(KeyEvent e){
        irany = e.getKeyCode();

        switch (irany) {
        case KeyEvent.VK_LEFT: //balra
            xv = -1;
            yv = 0;
        break;

        case KeyEvent.VK_UP: //fel
            xv = 0;
            yv = -1;
        break;

        case KeyEvent.VK_RIGHT: //jobbra
            xv = 1;
            yv = 0;
        break;

        case KeyEvent.VK_DOWN: //le
            xv = 0;
            yv = 1;
        break;

        default:
        }



        }

    @Override
    public void keyTyped(KeyEvent e){

    }

    @Override
    public void keyReleased(KeyEvent e){

    }



    static void setx(int ujx) {
        x = ujx;
    }
    static void sety(int ujy) {
        y = ujy;
    }
    static int getx() {
        return x;
    }
    static int gety() {
        return y;
    }

}

Palya.java:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

class Palya extends JPanel {

    @Override
    public void paintComponent (Graphics g) {
        super.paintComponent(g);

        Color c = new Color(130,130,130);
        g.setColor(c);
        g.fillRect(8,8,624,624);

        c = new Color(238,238,238);
        g.setColor(Color.WHITE);
        g.fillRect(30,30,578,578);

        Image kep = new ImageIcon("kocka.png").getImage();
        g.drawImage(kep,Mozgas.x,Mozgas.y,this);
    }
}

第二个(工作)代码:

import java.awt.*;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Ball extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
int x = 0, y = 0, velx =0, vely =0;



public static void main(String[] args) {

JFrame f = new JFrame();
Ball s = new Ball();
f.add(s);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600,400);
f.setVisible(true);

}






public Ball() {
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(x,y,50,30);
}

public void actionPerformed(ActionEvent e) {
if(x < 0)
{
velx=0;
x = 0;
}

if(x > 530)
{
velx=0;
x = 530;
}

if(y < 0)
{
vely=0;
y = 0;
}

if(y > 330)
{
vely=0;
y = 330;
}


x += velx;
y += vely;
repaint();
}

public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();

if (code == KeyEvent.VK_DOWN){
vely = 1;
velx = 0;
}
if (code == KeyEvent.VK_UP){
vely = -1;
velx = 0;
}
if (code == KeyEvent.VK_LEFT){
vely = 0;
velx = -1;
}
if (code == KeyEvent.VK_RIGHT){
vely = 0;
velx = 1;

}
}



public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
velx=0;
vely=0;
}


}

错误讯息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Mozgas.actionPerformed(Mozgas.java:56)
    at javax.swing.Timer.fireActionPerformed(Timer.java:313)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Mozgas.actionPerformed(Mozgas.java:56)
    at javax.swing.Timer.fireActionPerformed(Timer.java:313)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:245)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我的代码(第一个)工作正常,当“public Mozgas(){...}”中的所有内容都在“void menet(){...}”中,并且addKey ...,setFocus .. 。,setFocus ...,以“keret”开头。 (pl。:keret.addKeyListener(this))

我看到两个代码几乎是同一个方法。 我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

你的NullPointerException的原因是&#39; field&#39;调用&#39; repaint()&#39;时未初始化从它。

class Mozgas {
   public void actionPerformed(ActionEvent e) {
     field.repaint();

当您创建Mozgas&#39;定时器&#39;启动后触发actionPerformed方法之前 rajta.menet();启动&#39;字段&#39;会员。因此,您必须更正初始化对象和启动计时器的顺序。

另一个问题是您的应用程序正在主线程中运行,而计时器会调用您的“动作执行”&#39; EventDispatchingThread中的方法。您的GUI应用程序也应该在EDT中运行。

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
      start go = new start();
      go.main();
  });
}