import java.util.*;
import java.io.*;
import java.lang.String;class Process
{
  private int processID;//标识符
  private int priority;//优先级
  private int processstate;   //状态
  private String[] instructions=new String[100];//指令存储域
  
  private int Loadpoint=0;
  
  public void LOAD(String file)  //将进程的指令(从文件)加载到指令存储空间中
  {
  
  
 try{ 
  String line;
  FileReader fr=new FileReader(file);
  BufferedReader inFile=new BufferedReader(fr);
  
  int [] replace=new int[10];
  String [] Lable=new String[5];
  
  for(int p=0;p<5;p++)
   Lable[p]="0";
  
  String calllable="";
   
  int count=0;
    
  
  line=inFile.readLine();
  
  while(line!=null)
  {
    StringTokenizer tokenizer=new StringTokenizer(line);
    String [] a=new String[20];
    int i=0;
    
  
    while(tokenizer.hasMoreTokens())
  {
   a[i]=tokenizer.nextToken();
  
   i++;
  
  }
  
   if(a[0].equals(calllable))

{
        Loadpoint=60;
            String s="  ";
   int n=1;
   while(i>1)
   {
   s+=a[n]+" ";
   n++;
   i--;
   }
  
   instructions[Loadpoint]=s;
  
  
}
  else if(a[1].equals("ADD")||a[1].equals("DIV")||a[1].equals("INC")||
  a[1].equals("DEC")||a[1].equals("NEG")||a[1].equals("MUL")||
  a[1].equals("AND")||a[1].equals("OR")||a[1].equals("XOR")||
  a[1].equals("CMP")||a[1].equals("JMP")||a[1].equals("LOOPZ")||
  a[1].equals("LOOPNZ")||a[1].equals("LOOPE")||a[1].equals("LOOPNE")||
  a[1].equals("MOV")||a[1].equals("PUSH")||a[1].equals("POP")||
  a[1].equals("CALL")||a[1].equals("RET"))
  {
   replace[count]=Loadpoint;
  
   Lable[count]=a[0];
  
   String s="  ";
   int n=1;
   while(i>1)
   {
   s+=a[n]+" ";
   n++;
   i--;
   }
  
   instructions[Loadpoint]=s;
  
  
  }
  
else if(a[0].equals("JMP"))
{
  for(int q=0;!Lable[q].equals("0");q++)
  {
   if(a[1].equals(Lable[q]))
   {
   a[1]=String.valueOf(replace[q]);
   }
  }
  
instructions[Loadpoint]="  "+a[0]+" "+a[1];
    
        
}

else if(a[0].equals("LOOPZ"))
{
  for(int q=0;!Lable[q].equals("0");q++)
  {
   if(a[2].equals(Lable[q]))
   {
   a[2]=String.valueOf(replace[q]);
   }
  }
  
instructions[Loadpoint]="  "+a[0]+" "+a[1]+" "+a[2];
    
        
}

else if(a[0].equals("LOOPNZ"))
{
  for(int q=0;!Lable[q].equals("0");q++)
  {
   if(a[2].equals(Lable[q]))
   {
   a[2]=String.valueOf(replace[q]);
   }
  }
  
instructions[Loadpoint]="  "+a[0]+" "+a[1]+" "+a[2];
    
        
}

else if(a[0].equals("LOOPE"))
{
  for(int q=0;!Lable[q].equals("0");q++)
  {
   if(a[3].equals(Lable[q]))
   {
   a[3]=String.valueOf(replace[q]);
   }
  }
  
instructions[Loadpoint]="  "+a[0]+" "+a[1]+" "+a[2]+" "+a[3];
    
        
}

else if(a[0].equals("LOOPNE"))
{
  for(int q=0;!Lable[q].equals("0");q++)
  {
   if(a[3].equals(Lable[q]))
   {
   a[3]=String.valueOf(replace[q]);
   }
  }
  
instructions[Loadpoint]="  "+a[0]+" "+a[1]+" "+a[2]+" "+a[3];
    
        
}


   else if(a[0].equals("CALL"))
{
   calllable=a[1];
   a[1]=String.valueOf(60);
   
   instructions[Loadpoint]="  "+a[0]+" "+a[1];
}


  
   


  else instructions[Loadpoint] = line;
  
  line=inFile.readLine();
  Loadpoint++;  
  }
  
  }
   catch(NumberFormatException e){}
   catch(FileNotFoundException e){}
   catch(IOException e){}
  
  }
  
  
  public void SetprocessID(int id)
  {
   processID=id;
  }
  
  public int GetprocessID()
  {
   return processID;
  }
  
  public void Setpriority(int p)
  {
   priority=p;
  }
  
  public int Getpriority()
  {
   return priority;
  }
  
  
  public static void main(String args[])
  {
   Process p=new Process();
  
   p.LOAD("haha.txt");
  
   System.out.println(p.instructions[0]);
   System.out.println(p.instructions[1]);
   System.out.println(p.instructions[2]);
   System.out.println(p.instructions[3]);
   System.out.println(p.instructions[4]);
   System.out.println(p.instructions[5]);
   System.out.println(p.instructions[6]);
   System.out.println(p.instructions[7]);
   System.out.println(p.instructions[8]);
   System.out.println(p.instructions[9]);
   System.out.println(p.instructions[60]);
   System.out.println(p.instructions[61]);
   System.out.println(p.instructions[62]);
  
  
  }
}这段程序为什么有时能正常运行,有时不行啊,(第一遍应该可以运行,然后你把“haha.txt中的JMP T后面的内容删去,就不能
运行了,就会有异常,然后你把JMP T后的内容添上,还是不能运行,还是有异常”),大虾们帮帮忙啊
haha.txt中的内容如下:
T ADD R0 R1 R2
  AND R1 R2 # 3
  DIV R0 R1 R2
  ADD R0 R1 R2
  AND R1 R2 # 3
  JMP T
  DIV R0 R1 R2
  CALL M
  AND R1 R2 # 3
M DIV R0 R1 R2
  ADD R1 R2 R3
  RET .

解决方案 »

  1.   

     if(a[0].equals(calllable)) 你改成 if(callable.equals(a[0]))试试看
    其它类似的地方都改下
    还有,能不能把异常给贴出来
      

  2.   

    回一楼,(你可以自己试一下啊)
    异常如下:
    Exception in thread "main" java.lang.NullpointerException
           at Process.LOAD<Process.java:68>
           at Process.main<Process.java:224>
      

  3.   

    a[i]可能是null值,因为你用 stringToken...的时候,可能没有20个子串,而a的大小是20,所以会现null值,所以建议进行字符串比较的时候用这种形式"".equals( v);
    ""为常量, v为变量,这样就不会出现  null.equals(v)