使用netbean运行后,出现如下提示,第65行有问题run:
Enter the width of grid(width>=2):
Enter the width of grid(width>=2): 20
Enter the height of grid(height>=2):
Enter the height of grid(height>=2): 20
Enter the number of pixels in each grid square(pixels>=1):
Enter the number of pixels in each grid square(pixels>=1): 10
Exception in thread "main" java.lang.NullPointerException
at drawgrid.main(drawgrid.java:65)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*//**
*
* @author Administrator
*/import sheffield.*;
import java.util.*;
public class drawgrid { public static void main(String args[] ) {
int middle2;
int middle1;
int midd2;
int midd1;
int cross;
int a;
int b;
int c;
int d;
int e;
int f;
EasyReader keyboard = new EasyReader();
int width = keyboard.readInt("Enter the width of grid(width>=2): ");
int height = keyboard.readInt("Enter the height of grid(height>=2): ");
int pix = keyboard.readInt("Enter the number of pixels in each grid square(pixels>=1): ");
EasyGraphics g = new EasyGraphics(width*pix,height*pix);
if (width % 2 == 0)
{
middle2 = width/2;
}
else
{
middle2=(width+1)/2;
}
if (height % 2 == 0)
{
middle1 = height/2;
}
else
{
middle1=(height+1)/2;
}
midd2 = middle2 * pix;
midd1 = middle1 * pix;
int[][] mypoint = new int[width][height];//建立一个整形数据,用来存储每个点的位置 String[][] oldpoint = new String[width][height];//建立一个字符串数组,用于存储x,y 格式的已走过的点
g.moveTo(midd2,middle1 * pix); //移动到中心点
g.plot(midd2,middle1 * pix); //将中心点变黑
int z=0;
int num=0;
oldpoint[middle2][middle1]=middle2 + "$"+ middle1 ; //将中心点存入走过的点的数组
for (z=0;z<10;z++)
{
String[] crosses = new String[5];//一个可以存5个字符串的
if (z>1){
z=z-1;
}
if (middle2+1<=height-1 & middle2+1>=0 & middle1<=width-1 &middle1>=0){
65行: if ( oldpoint[middle2+1][middle1].length() == 0 | oldpoint[middle2+1][middle1]==null){ num = num + 1 ;
int temp1=middle2+1;
int temp2 = middle1;
crosses[num]=temp1+"$"+temp2;
}
}
if (middle2-1<=height-1 & middle2-1>=0 & middle1<=width-1 &middle1>=0){
if (oldpoint[middle2-1][middle1]==null | oldpoint[middle2-1][middle1].length() ==0){
num=num+1;
int temp1=middle2-1;
int temp2=middle1;
crosses[num]=temp1+"$"+temp2;
}
}
if (middle2<=height-1 & middle2>=0 & middle1+1<=width-1 &middle1+1>=0){
if (oldpoint[middle2][middle1+1]==null | oldpoint[middle2][middle1+1].length()==0){
num=num+1;
int temp1=middle2;
int temp2=middle1+1;
crosses[num]=temp1+"$"+temp2;
}
}
if (middle2<=height-1 & middle2>=0 & middle1-1<=width-1 &middle1-1>=0){
if (oldpoint[middle2][middle1-1]==null | oldpoint[middle2][middle1-1].length()==0){
num=num+1;
int temp1=middle2;
int temp2=middle1-1;
crosses[num]=temp1+"$"+temp2;
}
}
if (num>=1){
Random r=new Random();
cross = r.nextInt(num);
String temp = new String();
temp = crosses[cross];//获得crosses数组当前随机数下标的字符串值
a = temp.indexOf("$");//获得当前字符串中$所在的位置,从最前查
b = temp.lastIndexOf("$");//获得当前字符串中$所在的位置,从最后查
e = a-1;
f = Integer.parseInt(temp,10);
temp = temp.substring(0,e);
c = Integer.parseInt(temp,10);
d = Integer.parseInt(temp.substring(a+1,a+b-1));
oldpoint[c][d]=c + "$"+ d ;
g.lineTo(c*pix,d*pix);
middle2 = c;
middle1 = d; }
else
{
break;
} }
}
}
Enter the width of grid(width>=2):
Enter the width of grid(width>=2): 20
Enter the height of grid(height>=2):
Enter the height of grid(height>=2): 20
Enter the number of pixels in each grid square(pixels>=1):
Enter the number of pixels in each grid square(pixels>=1): 10
Exception in thread "main" java.lang.NullPointerException
at drawgrid.main(drawgrid.java:65)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*//**
*
* @author Administrator
*/import sheffield.*;
import java.util.*;
public class drawgrid { public static void main(String args[] ) {
int middle2;
int middle1;
int midd2;
int midd1;
int cross;
int a;
int b;
int c;
int d;
int e;
int f;
EasyReader keyboard = new EasyReader();
int width = keyboard.readInt("Enter the width of grid(width>=2): ");
int height = keyboard.readInt("Enter the height of grid(height>=2): ");
int pix = keyboard.readInt("Enter the number of pixels in each grid square(pixels>=1): ");
EasyGraphics g = new EasyGraphics(width*pix,height*pix);
if (width % 2 == 0)
{
middle2 = width/2;
}
else
{
middle2=(width+1)/2;
}
if (height % 2 == 0)
{
middle1 = height/2;
}
else
{
middle1=(height+1)/2;
}
midd2 = middle2 * pix;
midd1 = middle1 * pix;
int[][] mypoint = new int[width][height];//建立一个整形数据,用来存储每个点的位置 String[][] oldpoint = new String[width][height];//建立一个字符串数组,用于存储x,y 格式的已走过的点
g.moveTo(midd2,middle1 * pix); //移动到中心点
g.plot(midd2,middle1 * pix); //将中心点变黑
int z=0;
int num=0;
oldpoint[middle2][middle1]=middle2 + "$"+ middle1 ; //将中心点存入走过的点的数组
for (z=0;z<10;z++)
{
String[] crosses = new String[5];//一个可以存5个字符串的
if (z>1){
z=z-1;
}
if (middle2+1<=height-1 & middle2+1>=0 & middle1<=width-1 &middle1>=0){
65行: if ( oldpoint[middle2+1][middle1].length() == 0 | oldpoint[middle2+1][middle1]==null){ num = num + 1 ;
int temp1=middle2+1;
int temp2 = middle1;
crosses[num]=temp1+"$"+temp2;
}
}
if (middle2-1<=height-1 & middle2-1>=0 & middle1<=width-1 &middle1>=0){
if (oldpoint[middle2-1][middle1]==null | oldpoint[middle2-1][middle1].length() ==0){
num=num+1;
int temp1=middle2-1;
int temp2=middle1;
crosses[num]=temp1+"$"+temp2;
}
}
if (middle2<=height-1 & middle2>=0 & middle1+1<=width-1 &middle1+1>=0){
if (oldpoint[middle2][middle1+1]==null | oldpoint[middle2][middle1+1].length()==0){
num=num+1;
int temp1=middle2;
int temp2=middle1+1;
crosses[num]=temp1+"$"+temp2;
}
}
if (middle2<=height-1 & middle2>=0 & middle1-1<=width-1 &middle1-1>=0){
if (oldpoint[middle2][middle1-1]==null | oldpoint[middle2][middle1-1].length()==0){
num=num+1;
int temp1=middle2;
int temp2=middle1-1;
crosses[num]=temp1+"$"+temp2;
}
}
if (num>=1){
Random r=new Random();
cross = r.nextInt(num);
String temp = new String();
temp = crosses[cross];//获得crosses数组当前随机数下标的字符串值
a = temp.indexOf("$");//获得当前字符串中$所在的位置,从最前查
b = temp.lastIndexOf("$");//获得当前字符串中$所在的位置,从最后查
e = a-1;
f = Integer.parseInt(temp,10);
temp = temp.substring(0,e);
c = Integer.parseInt(temp,10);
d = Integer.parseInt(temp.substring(a+1,a+b-1));
oldpoint[c][d]=c + "$"+ d ;
g.lineTo(c*pix,d*pix);
middle2 = c;
middle1 = d; }
else
{
break;
} }
}
}
解决方案 »
- JAVA String 6个题目,谁给我讲讲是为什么呢
- 怎么才能学好java哪?
- 求助:java版本问题
- 新手问路:无法从静态上下文中引用非静态,程序
- 如何实现加checkbox的JTree显示?就象Eclipse里导出文件到Jar向导里选择包的树型目录
- 谁有java se 1.6 API 发给我一份
- jscrollpane 定位
- 大家都来说说自己公司的经理呀,有助身心健康哦!
- 所有的JFC对象都是线程不安全的,大家遇到过这方面的问题吗?
- 我新做一个Application,刚生成就出现下列错误提示,不知什么原因,请各位帮忙解决
- 关于hashset 与 string的一个问题
- javamail发附件,附件是从ftp服务器取的,怎么弄?
首先判断是不是null 如果是null调用.length()就空指针