Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 求教一个简单问题java.lang.NullPointerException
风狼





发贴: 3
积分: 0
于 2005-08-19 08:50 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
求教一个简单问题java.lang.NullPointerException

我是一个新手,调试一个小程序时出现了java.lang.NullPointerException异常

其代码是

import java.io.*;
class ArrayElementAngle
{
public static void main(String[] args)
{
int mArray[][]=new int[5][];
int nCounter=1;

for(int m=0;m<mArray.length;m++)
{
for(int n=0;n<nCounter;n++)
mArray[m][n]=nCounter;//在编译时也没有出现问题,在执行时说这一行有空指针异常

nCounter++;
}
for(int m=0;m<mArray.length;m++){
System.out.println("第"+m+"行->");
for(int n=0;n<mArray[m].length;n++)
System.out.println(" "+mArray[m][n]+" ");
System.out.println("\n");
}
}
}

mArray[m][n]=nCounter;//在javac时也没有出现问题,在执行时说这一行有空指针异常
请各位大虾指教,谢先!



作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
breezehou





发贴: 35
积分: 0
于 2005-08-19 09:18 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
import java.io.*;

class ArrayElementAngle {
  public static void main(String[] args) {
    int mArray[][] = new int[5][];
    int nCounter = 1;
    
    for (int m = 0; m < mArray.length; m++) {
      for (int n = 0; n < nCounter; n++) {
        mArray[m] = new int[n+1];
        mArray[m][n] = nCounter;
      }
      nCounter++;
    }
    
    for (int m = 0; m < mArray.length; m++) {
      System.out.println("The " + m + "line->");
      for (int n = 0; n < mArray[m].length; n++)
        System.out.println(" " + mArray[m][n] + " ");
      System.out.println("\n");
    }
  }
}



作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
风狼





发贴: 3
积分: 0
于 2005-08-19 12:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
果然可以了,谢了先,
再问一下,是不是因为再用之前没为其申请空间?



作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
breezehou





发贴: 35
积分: 0
于 2005-08-19 13:48 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
二维数组可以理解为指向数组的数组


作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-08-20 18:24 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Don't double post!!

leiofll replied to your duplicated post, which is deleted:
在for循环中,nCounter是外部变量,所以,nCounter++不会起到作用,把nCounter声明在for循环里边,试一下



作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
acl2005





发贴: 54
积分: 0
于 2005-08-21 21:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
元老请教您:int i=0;
for(;true;){
for(;i<10;i++){
}
}
在这样的情况下"i"也属于外部变量吗?然后做第一次循环的时候是从初始值开始循环的,我发现"while"循环的时候如果先定义了初始变量后,如果在循环体里面有"i++"的话,那么他就执行这次"i++".不像"for"循环等到下一次在执行.这也是它们的语法区别吗??
问题可能太过简单,不过怕以后走太多的弯路,还请元老指点.
                       这里谢过了!!!


why edited on 2005-08-22 07:17

作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
acl2005





发贴: 54
积分: 0
于 2005-08-21 21:57 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不好意思,上边的应该是for(;trueWink不知为什么变了


作者 Re:求教一个简单问题java.lang.NullPointerException [Re:acl2005]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-08-22 07:22 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
acl2005 wrote:
不好意思,上边的应该是for(;trueWink不知为什么变了

use [ code ] or check "Disable Smileys"


No, i is not local (to the scope of the for construct) in your example.
i.e. 属于外部变量

我发现"while"循环的时候如果先定义了初始变量后,如果在循环体里面有"i++"的话,那么他就执行这次"i++".不像"for"循环等到下一次在执行.这也是它们的语法区别吗??

I don't understand what you're trying to ask. Sorry.
Please use some code example.



作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
acl2005





发贴: 54
积分: 0
于 2005-08-22 22:21 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢您元老,麻烦您了.下面是两个例题请您看一下

//: LabeledFor.java
// Java’s "labeled for loop"

public class LabeledFor {
public static void main(String[] args) {
int i = 0;
outer: // Can't have statements here
for(; true ;) { // infinite loop
inner: // Can't have statements here
for(; i < 10; i++) {
prt("i = " + i);
if(i == 2) {
prt("continue");
continue;
}
if(i == 3) {
prt("break");
i++; // Otherwise i never
// gets incremented.
break;
}
if(i == 7) {
prt("continue outer");
i++; // Otherwise i never
// gets incremented.
continue outer;
}
if(i == 8) {
prt("break outer");
break outer;
}
for(int k = 0; k < 5; k++) {
if(k == 3) {
prt("continue inner");
continue inner;
}
}
}
}
// Can't break or continue
// to labels here
}
static void prt(String s) {
System.out.println(s);
}
} ///:~


上面这个例子中int i=0会先用这个初始值先循环一次
而下面这个例子中int i=0运行的时候没有用初始值循环.而是先用了i++;
这是两个循环之间("for";"while")的差别吗?请元老解释一下.

//: LabeledWhile.java
// Java's "labeled while" loop

public class LabeledWhile {
public static void main(String[] args) {
int i = 0;
outer:
while(true) {
prt("Outer while loop");
while(true) {
i++;
prt("i = " + i);
if(i == 1) {
prt("continue");
continue;
}
if(i == 3) {
prt("continue outer");
continue outer;
}
if(i == 5) {
prt("break");
break;
}
if(i == 7) {
prt("break outer");
break outer;
}
}
}
}
static void prt(String s) {
System.out.println(s);
}
} ///:~


{ Please do use [ code ] ! }


why edited on 2005-08-23 00:07

作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-08-23 00:40 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
> 上面这个例子中int i=0会先用这个初始值先循环一次
> 而下面这个例子中int i=0运行的时候没有用初始值循环.而是先用了i++;
很坦白說,敝人根本看不明白閣下在說啥(當然敝人可以猜猜看,但何必呢?)
更完全提不起精神看閣下那段例子
(心想:#$%&幹啥用這麼長卻其實與主旨無關的例子)
自然也沒有辦法把這本來十分簡單的東東說得明白.Embaressed

看看
http://www.cjsdn.net/post/view?bid=1&id=124764
會不會幫助到閣下.


why edited on 2005-08-23 00:59

作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
acl2005





发贴: 54
积分: 0
于 2005-08-23 21:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
元老说的是啊,把简单的事情弄复杂了反倒自己进入了误区.
也许在不断的学习当中,自己会弄明白的!
在这里再次感谢元老了!!!!!!!



作者 Re: for vs while [Re:风狼]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-08-23 22:16 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

for (A; B; C) {
E1;
...
En;
}


is equivalent to


{
A;
while (B) {
E1;
...
En;
C;
}
}


BTW, I seldom use while, but like to use do while


why edited on 2006-08-12 00:37

作者 Re:求教一个简单问题java.lang.NullPointerException [Re:风狼]
acl2005





发贴: 54
积分: 0
于 2005-08-24 21:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我看过了您留的哪个帖子了,我看大概有百分之90的朋友都喜欢用FOR啊
呵!!
我个人也对FOR有好感啊!!!


   对了元老:就上面的两个例子而言
如果while循环是这样那么还和您给的for循环相同吗?

{
A;
while(B){
C;
E1;
...
En;
}
}


why edited on 2006-08-12 00:48

作者 Re:求教一个简单问题java.lang.NullPointerException [Re:acl2005]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-08-12 00:47 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
acl2005 wrote:
如果while循环是这样那么还和您给的for循环相同吗?

{
A;
while(B){
C;
E1;
...
En;
}
}




for (A; B; ) {
C;
E1;
...
En;
}




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923