我试图让我的问题上次得到解决而且没有成功,因为我真的搞砸了整个代码。 现在,我决定尝试从头开始重写我的整个代码,但是我再次遇到了NPE错误。 我明白这意味着什么,什么时候发生,但有人可以告诉我,我在这里做错了什么? 我对所有建议持开放态度,但在你批评程序编写方式之前,请尝试解释我做错了什么。 当我在另一个类中创建下面的类的实例时,我会尝试调用update()或getxposition方法。 如果您需要任何附加信息,请告诉我。提前致谢! 编辑:(对不起,我忘了跟踪)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at game.StartingClass.paint(StartingClass.java:147)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "Thread-3" java.lang.NullPointerException
at game.StartingClass.run(StartingClass.java:67)
at java.lang.Thread.run(Unknown Source)
代码:
package game;
public class Character
{
private int xPosition = 5;
private int yPosition = 380;
private int moveSpeed = 3;
private int jumpSpeed = 2;
private boolean isMovingRight = false;
private boolean isMovingLeft = false;
public void update()
{
if (isMovingRight == true) //handles moving right
{
xPosition = xPosition + moveSpeed;
}
if (isMovingLeft == true) //handles moving left
{
xPosition = xPosition - moveSpeed;
}
}
public int getxPosition() {
return xPosition;
}
public int getyPosition() {
return yPosition;
}
public boolean isMovingRight() {
return isMovingRight;
}
public boolean isMovingLeft() {
return isMovingLeft;
}
public void setxPosition(int xPosition) {
this.xPosition = xPosition;
}
public void setyPosition(int yPosition) {
this.yPosition = yPosition;
}
public void setMovingRight(boolean isMovingRight) {
this.isMovingRight = isMovingRight;
}
public void setMovingLeft(boolean isMovingLeft) {
this.isMovingLeft = isMovingLeft;
}
}
这是类,我在:
中创建了Character实例package game;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
public class StartingClass extends Applet implements Runnable,KeyListener
{
private Graphics second;
private Image image,prasac;
private URL base;
private Character pig;
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init()
{
setSize(800,480); //Dimenzije Appleta
setBackground(Color.BLACK);
setVisible(true);
addKeyListener(this);
setFocusable(true);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Game Demo");
try {
base=getDocumentBase();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Uzimanje slike
prasac = getImage(base,"sprites/Main.png");
}
@Override
public void start() //Start metoda
{
Character pig = new Character();
Thread thread = new Thread(this);
thread.start();
}
@Override
public void stop() //Stop metoda
{
}
@Override
public void destroy() //Destroy metoda
{
}
@Override
public void run() //Run metoda, od interfacea Runnable, potrebna za thread.
{
while(true)
{
pig.update();
repaint();
try
{
Thread.sleep(17);
}
catch (InterruptedException e)
{
System.out.println("Fatal error!");
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
System.out.println("Gore");
break;
case KeyEvent.VK_DOWN:
System.out.println("Dolje");
break;
case KeyEvent.VK_LEFT:
System.out.println("Lijevo");
break;
case KeyEvent.VK_RIGHT:
System.out.println("Desno");
break;
}
}
@Override
public void keyReleased(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
System.out.println("StopGore");
break;
case KeyEvent.VK_DOWN:
System.out.println("StopDolje");
break;
case KeyEvent.VK_LEFT:
System.out.println("StopLijevo");
break;
case KeyEvent.VK_RIGHT:
System.out.println("StopDesno");
break;
}
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void update(Graphics g)
{
if (image == null) {
image = createImage(this.getWidth(), this.getHeight());
second = image.getGraphics();
}
second.setColor(getBackground());
second.fillRect(0, 0, getWidth(), getHeight());
second.setColor(getForeground());
paint(second);
g.drawImage(image, 0, 0, this);
}
@Override
public void paint(Graphics g)
{
g.drawImage(prasac, pig.getxPosition(), pig.getyPosition(), this);
}
public Character getPig() {
return pig;
}
public void setPig(Character pig) {
this.pig = pig;
}
}
答案 0 :(得分:1)
代码在当前状态下工作。您可能不应该将您的类命名为Character,因为它是原始字符的包装类的名称(如果您在任何使用game.Character的类中使用了java.lang.Character,您必须使用完全限定的名称其中一个)。字段级原语布尔值的默认值为false,因此您无需将它们设置为false。你真的应该改变处理更新方法的方式。就像现在一样,可以同时左右移动,然后更新将无效。另外,你可以使用
if(isMovingRight) { ... }
因为isMovingRight已经是一个布尔值。
编辑:这是在他发布了stacktrace和StartingClass
之前发布的更新后:
猪是空的
@Override
public void start() {
pig = new Character();
Thread thread = new Thread(this);
thread.start();
}