| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1395 人关注过本帖
标题:无语法错误但运行却抛出nullpointexception异常,请帮忙
取消只看楼主 加入收藏
fanschun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-12
收藏
 问题点数:0 回复次数:2 
无语法错误但运行却抛出nullpointexception异常,请帮忙
我编的游戏程序没语法错误可运行时,却总是抛出异常nullpoinexception,问了很多人都没有得到答案,请各位帮帮忙,万分感谢 *附程序如下* import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; import java.util.*; import java.io.*; public class mycanvas extends GameCanvas implements Runnable{ private Sprite yuchu; private long frameDelay; public boolean sleeping; public int x=0,y=0; private Display display; private Random rand; public mycanvas(Display d){ super(true); display=d; frameDelay=33; } public void start(){ display.setCurrent(this); rand=new Random(); sleeping=false; try{ yuchu=new Sprite(Image.createImage("1.png")); }catch(IOException e){System.err.println("failure");} yuchun.setPosition(0,0); Thread t=new Thread(this); t.start(); } public void run(){ Graphics g=getGraphics(); while(!sleeping){ update(); draw(g); try{Thread.sleep(frameDelay); }catch(InterruptedException e){} } } public void stop(){ sleeping=true; } public void update(){ if(rand.nextInt()%5==0){ int a=Math.max(-8,rand.nextInt()%2); x=Math.min(a,8); int b=Math.max(-8,rand.nextInt()%2); y=Math.min(b,8); yuchu.move(x,y); } if(yuchu.getX()<=-yuchu.getWidth()) yuchu.setPosition(getWidth(),yuchu.getY()); else if(yuchun.getX()>=getWidth()) yuchu.setPosition(-yuchu.getWidth(),yuchu.getY()); if(yuchu.getY()<=-yuchu.getHeight()) yuchu.setPosition(yuchu.getX(),getHeight()); else if(yuchun.getY()>=getHeight()) yuchu.setPosition(yuchu.getX(),-yuchu.getHeight()); } public void draw(Graphics g){ g.setColor(0,0,0); g.fillRect(0,0,getWidth(),getHeight()); yuchu.paint(g); flushGraphics(); } } import javax.microedition.lcdui.*; import java.util.*; import java.io.*; import javax.microedition.midlet.*; public class yuchun extends MIDlet implements CommandListener{ private Command cmd_exit; public Display display; mycanvas canvas; public yuchun(){ canvas=new mycanvas(display.getDisplay(this)); } public void startApp(){ Command cmd_exit=new Command("exit",Command.EXIT,1); canvas.addCommand(cmd_exit); canvas.setCommandListener(this); canvas.start(); } public void pauseApp(){} public void destroyApp(boolean a){} public void commandAction(Command c,Displayable d){ if(c==cmd_exit){ canvas.stop(); destroyApp(false); notifyDestroyed(); } } } 图片为一张28*40像素png格式的图片
共有0回帖 当前第1页(共1页) 1
搜索更多相关主题的帖子: 语法错误 import java quot public 
2005-10-12 13:20
fanschun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-12
收藏
得分:0 
我定义了GameCanvas类,类名为mycanvas,代码语法应该没错误(此程序实现一图片在屏幕上随机移动)
2005-10-12 23:37
fanschun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-10-12
收藏
得分:0 

我的代码给全了啊! 我把我的程序代码和运行时抛出的异常标示如下,麻烦大家看一下,我实在是被这一类问题烦死了,谢谢大家帮忙一下吧!!! (本程序中用到的为一张28*40像素png格式的图片) import javax.microedition.lcdui.*; import java.util.*; import java.io.*; import javax.microedition.midlet.*;

public class yuchun extends MIDlet implements CommandListener{ private Command cmd_exit; public Display display; mycanvas canvas; public yuchun(){ canvas=new mycanvas(display.getDisplay(this)); } public void startApp(){ Command cmd_exit=new Command("exit",Command.EXIT,1); canvas.addCommand(cmd_exit); canvas.setCommandListener(this); canvas.start(); } public void pauseApp(){} public void destroyApp(boolean a){} public void commandAction(Command c,Displayable d){ if(c==cmd_exit){ canvas.stop(); destroyApp(false); notifyDestroyed(); } } }//class yuchun

import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; import java.util.*; import java.io.*;

public class mycanvas extends GameCanvas implements Runnable{ //定义的GameCanvas一个类mycanvas private Sprite yuchu; private long frameDelay; public boolean sleeping; public int x=0,y=0; private Display display; private Random rand;

public mycanvas(Display d){ super(true); display=d; frameDelay=33; }

public void start(){ display.setCurrent(this); rand=new Random(); sleeping=false; try{ yuchu=new Sprite(Image.createImage("1.png")); }catch(IOException e){System.err.println("failure");} yuchu.setPosition(0,0); Thread t=new Thread(this); t.start(); }

public void run(){ Graphics g=getGraphics(); while(!sleeping){ update(); draw(g); try{Thread.sleep(frameDelay); }catch(InterruptedException e){} } }

public void stop(){ sleeping=true; }

public void update(){ if(rand.nextInt()%5==0){ int a=Math.max(-8,rand.nextInt()%2); x=Math.min(a,8); int b=Math.max(-8,rand.nextInt()%2); y=Math.min(b,8); yuchu.move(x,y); }

if(yuchu.getX()<=-yuchu.getWidth()) // 此段代码为如果精灵超出了显示范围,就折回屏幕的另一端 yuchu.setPosition(getWidth(),yuchu.getY()); else if(yuchu.getX()>=getWidth()) yuchu.setPosition(-yuchu.getWidth(),yuchu.getY()); if(yuchu.getY()<=-yuchu.getHeight()) yuchu.setPosition(yuchu.getX(),getHeight()); else if(yuchu.getY()>=getHeight()) yuchu.setPosition(yuchu.getX(),-yuchu.getHeight()); }

public void draw(Graphics g){ g.setColor(0,0,0); g.fillRect(0,0,getWidth(),getHeight()); yuchu.paint(g); flushGraphics(); } }//class mycanvas 以下为程序在运行过程中抛出的异常:

项目设置已保存 正在生成 "pro1" 生成完成 正在通过存储根 DefaultColorPhone 来运行 failure startApp threw an Exception java.lang.NullPointerException java.lang.NullPointerException at mycanvas.start(+58) at yuchun.startApp(+36) at javax.microedition.midlet.MIDletProxy.startApp(+7) at com.sun.midp.midlet.Scheduler.schedule(+270) at com.sun.midp.main.Main.runLocalClass(+28) at com.sun.midp.main.Main.main(+116)

2005-10-13 12:41
快速回复:无语法错误但运行却抛出nullpointexception异常,请帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038012 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved