当前位置: 代码迷 >> Java相关 >> [求助]编译正常,但是运行出错?
  详细解决方案

[求助]编译正常,但是运行出错?

热度:273   发布时间:2005-10-17 20:26:00.0
[求助]编译正常,但是运行出错?
public class JosephNode
{
public int ID;  
public int TurnID;  //˳Ðò
public int next; //Ï൱ÓÚC++µÄÖ¸Õë
};


public class Joseph
{
public static void main(String[] arg)
{
  JosephNode node[] = new JosephNode[12];
  for(int i=0;i<node.length;i++)
  {
   node[i].ID = node[i].TurnID = i+1;
   node[i].next = (i+1)%node.length;
  }
  int left = 12,count = 0,i=0;
  while(left==1)
  {
   node[i].TurnID = (count++)%5;
   if(node[i].TurnID==0)
   {
    node[(left+i-1)%12].next++;
    left--;
   }
   i = node[i].next;
  }
  System.out.println("the "+i+" will out");
}
};


//出错提示
Exception in thread "main" java.lang.NullPointerException
        at Joseph.main(Joseph.java:8)
Press any key to continue...
搜索更多相关的解决方案: 编译  运行  

----------------解决方案--------------------------------------------------------
public class JosephNode          //main不在这个class里,删掉public
{
public int ID;  
public int TurnID;  //˳Ðò
public int next; //Ï൱ÓÚC++µÄÖ¸Õë
};                        //不是C++,类结尾不需要;
----------------解决方案--------------------------------------------------------
按你说的改了,出错提示:

Exception in thread "main" java.lang.NoSuchMethodError: main
Press any key to continue...
----------------解决方案--------------------------------------------------------
你的java文件的名字是JosephNode还是Joseph?应该要叫Joseph
----------------解决方案--------------------------------------------------------
我俩个文件放在同一个文件夹里的.
----------------解决方案--------------------------------------------------------
2个文件?
那你的入口文件必须是Joseph
----------------解决方案--------------------------------------------------------

//我修改了一下 //结果是 the 0 will out public class JosephNode { public int ID; public int TurnID; //˳Ðò public int next; //Ï൱ÓÚC++µÄÖ¸Õë } public class Joseph { public static void main(String[] arg) { try{

JosephNode [] node = new JosephNode[12]; for(int i = 0; i<12;i++){ node[i] = new JosephNode(); } for(int i=0;i<node.length;i++) { node[i].ID = i+1; node[i].TurnID = i+1; node[i].next =(int) ((i+1)%node.length); } int left = 12,count = 0,i=0; while(left==1) { node[i].TurnID = (int)(count++)%5; if(node[i].TurnID==0) { node[(left+i-1)%12].next++; left--; } i = node[i].next; } System.out.println("the "+i+" will out"); } catch(NullPointerException e){ e.printStackTrace(); }

} }


----------------解决方案--------------------------------------------------------
可以不调用异常处理的,主要问题是我没有把下面那句加上.
   node[i] = new JosephNode();
----------------解决方案--------------------------------------------------------
是不用调用异常,只是我刚开始时就调用了,后来修改了,就一直没改掉
----------------解决方案--------------------------------------------------------
一个文件里,只能有一个PUBLIC 类的吧!
----------------解决方案--------------------------------------------------------
  相关解决方案