我知道这可能是一个非常愚蠢的问题,但我正在测试drJav中的Interactions窗格。
我创建一个String变量,然后尝试对其执行.length
:
String fish;
String[] hooo;
hooo.length
然后我收到一个错误,这让我很困惑:
edu.rice.cs.util.UnexpectedException: java.lang.NullPointerException
at edu.rice.cs.drjava.model.repl.newjvm.MainJVM$ResultHandler.forUnexpectedException(MainJVM.java:1045)
at edu.rice.cs.drjava.model.repl.newjvm.MainJVM$ResultHandler.forUnexpectedException(MainJVM.java:992)
at edu.rice.cs.drjava.model.repl.newjvm.InterpretResult$UnexpectedExceptionResult.apply(InterpretResult.java:111)
at edu.rice.cs.drjava.model.repl.newjvm.MainJVM.interpret(MainJVM.java:351)
at edu.rice.cs.drjava.model.repl.RMIInteractionsModel._interpret(RMIInteractionsModel.java:75)
at edu.rice.cs.drjava.model.repl.InteractionsModel.interpret(InteractionsModel.java:291)
at edu.rice.cs.drjava.model.repl.InteractionsModel$2$1.run(InteractionsModel.java:241)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at java.lang.reflect.Array.getLength(Native Method)
at edu.rice.cs.dynamicjava.symbol.ArrayLengthField$1.value(ArrayLengthField.java:27)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:56)
at koala.dynamicjava.tree.ObjectFieldAccess.acceptVisitor(ObjectFieldAccess.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
... 1 more
Caused by:
java.lang.NullPointerException
at java.lang.reflect.Array.getLength(Native Method)
at edu.rice.cs.dynamicjava.symbol.ArrayLengthField$1.value(ArrayLengthField.java:27)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:56)
at koala.dynamicjava.tree.ObjectFieldAccess.acceptVisitor(ObjectFieldAccess.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Thank You,
我知道这是一个空阵列,但我希望它能告诉我 - 即0
应该出现。
答案 0 :(得分:5)
您没有为hooo
分配值,因此其值为null
。当您尝试访问null
对象的属性时,会抛出NullPointerException
。
相反,请尝试以下内容。
String fish;
String[] hooo = {"Hello!", "Let's be friends."};
hooo.length;
答案 1 :(得分:2)
因为在这里你没有初始化变量hooo
,这就是它给出错误的原因。你可以这样做
String[] hooo = new String[0];
或
String[] hooo = {"hooo", "hoooo", "hoooooooooooo"};
答案 2 :(得分:0)
仅通过声明变量不会初始化它!您需要创建一个新的数组对象并分配给它。