急: NullPointerException

yorkzjy 2010-01-31 05:12:30
这个程序是用来排序的(效率不好,作业来的),编译通过,运行时候说什么main 的 NullPointerException 什么的,请各位指教了,这是我第一个用java写的程序,对于很多java的机制都不懂。
类部分没问题了,主要是main部分有问题。

先谢谢大家了




import java.util.*;
import java.io.*;
import java.lang.*;

//*******************************ObjArraySuperclass part***********************

class ObjArraySuperclass
{
private static final int DEFAULT_SIZE = 20;

private Object[] theArray;


public ObjArraySuperclass()
{
theArray = new Object[DEFAULT_SIZE];
}

public ObjArraySuperclass( int size )
{
theArray = new Object[size];
}

public ObjArraySuperclass( ObjArraySuperclass theOriginal ) // copy constructor
{
if ( theOriginal == null )
{
System.out.println("fatal error. null pointer in copy constructor");
System.exit(0);
}
else
{
theArray = new Object[theOriginal.theArray.length];
for (int i = 0; i < theArray.length; i++)
theArray[i] = theOriginal.theArray[i];
}
}


public boolean equals( Object theOther )
{
if ( theOther == null )
return false;
else if ( theOther == this )
return true;
else if ( getClass() != theOther.getClass())
return false;
else
{
ObjArraySuperclass o = (ObjArraySuperclass)theOther;
if ( theArray.length != o.theArray.length ) return false;
for ( int i = 0; i < theArray.length; i++ )
if ( theArray[i] != o.theArray[i] ) return false;
}
return true;
}

public int length()
{
return theArray.length;
}

public Object get( int index )
{
if ( index >= 0 && index < theArray.length )
return theArray[index];
else
return Integer.MIN_VALUE;
}

public boolean put( int index, Object value )//
{
if ( index >= 0 && index < theArray.length )
{
theArray[index] = value;
return true;
}
else
return false;
}






}
//end of superclass


//*******************************deque part************************************
class deque extends ObjArraySuperclass
{
enum ErrorMessage{ OVERFLOW, UNDERFLOW, COPYERROR };

private int front = 0;
private int rear = 0;
public deque()
{
super();
}

public deque(int size)
{
super(size);
}

public deque(deque theOther)
{
super(theOther);
this.front = theOther.front;
this.rear = theOther.rear;

}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public Object remove()
{
if ( isNotEmpty() )
{
front = (front+1) % super.length();
return super.get(front);
}
else
{
QueueError( ErrorMessage.UNDERFLOW );
return null;
}
}


public Object removeRear()
{
if (isNotEmpty())
{
rear = (rear-1)%(super.length());//the super class has already has the length() method
return super.get(rear);//return the value
}
else
{
QueueError(ErrorMessage.UNDERFLOW);
return null;
}



}
public Object peekRear()
{
if(isNotEmpty())
{
return super.get(rear);
}
else
{
QueueError(ErrorMessage.UNDERFLOW);
return null;
}
}
public Object peek()
{
if(isNotEmpty())
{
return super.get(front);
}
else
{
QueueError(ErrorMessage.UNDERFLOW);
return null;
}
}
public void insert(Object value)
{
if ( isNotFull() )
{
rear = (rear+1) % super.length();//this is the basic operation of calculating the loc
super.put(rear,value);
}
else
QueueError( ErrorMessage.OVERFLOW );//this is defined in this class
}

public void addFront(Object value)
{
if (isNotFull())
{
front = (front-1) % super.length();
super.put(front,value);

}
else
QueueError( ErrorMessage.OVERFLOW );



}

public void push(Object value)//same as addInFront() method
{
if (isNotFull())
{
front = (front-1) % super.length();
super.put(front,value);

}
else
QueueError( ErrorMessage.OVERFLOW );

}
public Object pop() // same as remove() method
{

if ( isNotEmpty() )
{
front = (front+1) % super.length();
return super.get(front);
}
else
{
QueueError( ErrorMessage.UNDERFLOW );
return null;//empty
}


}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public boolean isEmpty()
{
return front == rear;
}

public boolean isFull()
{
return (rear+1)%super.length() == front;
}

public boolean isNotFull()
{
return !isEmpty();
}

public boolean isNotEmpty()
{
return !isFull();
}

private void QueueError( ErrorMessage msg )
{
switch ( msg )
{
case UNDERFLOW:
System.out.println("QueueError: Queue Underflow" );
break;

case OVERFLOW:
System.out.println("QueueError: Queue Overflow" );
break;

case COPYERROR:
System.out.println("QueueError: null pointer sent of copy constructor" );
break;
}
System.exit(0);
}

public String toString()
{

String theString = "\tfront: " + front + " rear: "
+ rear + " contents: " ;
int loc = front;

if ( isNotEmpty() )
do
{
loc = (loc+1) % super.length();
theString = theString + super.get(loc) + " ";
}
while ( loc != rear );

theString = theString + "\n";
return theString;
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


}
//end of deque definition

//*******************************client part***********************************
public class dequeClient
{
public static void main(String[] args) throws NullPointerException
{

try
{
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
String theLine;
Integer theChar;
deque theQueue = new deque();
deque theStack = new deque();



while((theLine = br.readLine())!=null)//read a line store it in to theLine
{
System.out.println("bofore the peration: "+theLine);
for(int i=0; i<theLine.length(); i++)//this is to compute the opeartions
{
theChar = (Integer)(int)theLine.charAt(i);
while(theChar > (Integer )theQueue.peekRear())
{
Integer tempChar = (Integer )theQueue.removeRear();
theStack.push(tempChar);


}
while(theChar < (Integer )theStack.peek())
{
Integer tempChar = (Integer )theStack.pop();
theQueue.insert(tempChar);
}

//insert the new letter onto the rear of queue
theQueue.insert(theChar);

Integer temp;
while((temp = (Integer )theStack.pop())!= null )
{
theQueue.insert(temp);
}



}
System.out.println("after the opeartion: "+theQueue.toString()+"\n");




}





}
catch(IOException e)
{
System.out.println("IOexception ");
System.exit(-1);
System.out.println("ioException occurs");
System.exit(-1);
}


}
}

...全文
522 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
yorkzjy 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 xiesisi3 的回复:]
LZ问问题的时候要把运行后报的异常代码,源码都发出来才行,这样才能帮你看是错在哪里
[/Quote]

嗯,下次一定,谢谢了
xiesisi3 2010-02-02
  • 打赏
  • 举报
回复
LZ问问题的时候要把运行后报的异常代码,源码都发出来才行,这样才能帮你看是错在哪里
yorkzjy 2010-02-02
  • 打赏
  • 举报
回复
这个程序我已经重写了,不考虑多态之类的了,初学java,还是不要搞的太复杂。
yorkzjy 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 doniks 的回复:]
lz,怎么我运行你的程序后,在console上打印出‘IOexception’
有异常么?


[/Quote]

应该有
yorkzjy 2010-02-02
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 kuyu0518 的回复:]
楼主能把你的data.txt文件贴出来嘛
Java codetheChar= (Integer)(int)theLine.charAt(i);
这个在char转行为int类型的时候转化为ASCII码int数据,这个不太懂楼主想做什么样的操作
跟了下代码
while(theChar > (Integer )theQueue.peekRear())
--->
第一次循环
public boolean isFull()
    {
        return (rear+1)%super.length() == front;
    }
此时返回false
--->

public Object peekRear()
    {
        if(!isFull())
        {
         return super.get(rear);        }
        else
        {
            QueueError(ErrorMessage.UNDERFLOW);
            return null;
        }
    }
     public Object get( int index )
    {
        if ( index >= 0 && index < theArray.length )
            return theArray[index];
        else
            return Integer.MIN_VALUE;
    }
此时theArray还为添加内容,此时会返回null
while(theChar > (Integer )theQueue.peekRear())
(Integer )null会报NullPointerException异常
[/Quote]

请教怎么跟踪,啥工具?
doniks 2010-02-01
  • 打赏
  • 举报
回复
lz,怎么我运行你的程序后,在console上打印出‘IOexception’
有异常么?

yorkzjy 2010-01-31
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 w3329307 的回复:]
不会用开发工具呀
LZ你玩高深呀,这样的话没人能解决的
[/Quote]

不敢玩高深啊,大哥。

额,我还不会用开发工具,只能酱紫了。


嗯,这个问题解决了。(还有下个)。。。。。。。。。。
w3329307 2010-01-31
  • 打赏
  • 举报
回复
不会用开发工具呀
LZ你玩高深呀,这样的话没人能解决的
gloomyfish 2010-01-31
  • 打赏
  • 举报
回复
也是学计算机专业的 ,饿的神!
yorkzjy 2010-01-31
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 javaalpha 的回复:]
楼主 你的异常 信息是什么啊?

贴异常信息看看

根据异常提示 找具体的代码行就可以了。
[/Quote]

我用的是cmd,直接javac编译。

325行,额,但是水平有限,解决不了


JavaAlpha 2010-01-31
  • 打赏
  • 举报
回复
你用的是什么开发工具啊?

Eclipse或MyEclipse直接可以提示异常信息的
kuyu0518 2010-01-31
  • 打赏
  • 举报
回复
楼主能把你的data.txt文件贴出来嘛
theChar = (Integer)(int)theLine.charAt(i);

这个在char转行为int类型的时候转化为ASCII码int数据,这个不太懂楼主想做什么样的操作
跟了下代码
while(theChar > (Integer )theQueue.peekRear())
--->
第一次循环
public boolean isFull()
{
return (rear+1)%super.length() == front;
}
此时返回false
--->

public Object peekRear()
{
if(!isFull())
{
return super.get(rear); }
else
{
QueueError(ErrorMessage.UNDERFLOW);
return null;
}
}
public Object get( int index )
{
if ( index >= 0 && index < theArray.length )
return theArray[index];
else
return Integer.MIN_VALUE;
}
此时theArray还为添加内容,此时会返回null
while(theChar > (Integer )theQueue.peekRear())
(Integer )null会报NullPointerException异常
JavaAlpha 2010-01-31
  • 打赏
  • 举报
回复
楼主 你的异常 信息是什么啊?

贴异常信息看看

根据异常提示 找具体的代码行就可以了。
yorkzjy 2010-01-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhwh 的回复:]
是不是data.txt找不到啊。
[/Quote]

不是的
yorkzjy 2010-01-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 config_man 的回复:]
看看报的空指针异常信息,报在哪行就查哪行。
[/Quote]

不知道怎么加阿,大佬
Mybeautiful 2010-01-31
  • 打赏
  • 举报
回复
最外层调用方法Try, Catch下,然后把异常打印出来。
fancy161 2010-01-31
  • 打赏
  • 举报
回复
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
在你c盘建个 date.txt 随便输点东西保存
然后

BufferedReader br = new BufferedReader(new FileReader("c:\data.txt"));
kuyu0518 2010-01-31
  • 打赏
  • 举报
回复
运行了一下明明报的是找不到文件嘛
java.io.FileNotFoundException: data.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at dequeClient.main(dequeClient.java:304)
IOexception
zhwh 2010-01-31
  • 打赏
  • 举报
回复
是不是data.txt找不到啊。
l5854537x 2010-01-31
  • 打赏
  • 举报
回复
看看哪个空指针在那里,改过来就好了,是不是 一些变量已经定义成变量,有在方法中定义了
加载更多回复(4)

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧