-1

帮我解决这段代码,我得到运行时异常错误 java.lang.nullpointerexception 请帮助我解决这个问题以及如何使用 java 程序访问桌面上的任何文件,就像我想访问桌面上或桌面上的任何图像或文本文件一样任何其他折叠我应该写什么代码。仅供参考,我是菜鸟。

import java.io.*;
class jarvis
{
public static void main(String args[])
{
String i=null,j=null,k=null,l=null,m=null,n=null,o=null,a=null,q=null;
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Command me: ");
if(i.equals("jarvis you there?"))
{
i=br.readLine();

System.out.println("at your service sir");
}
System.out.println("Command me: ");
if(j.equals("run command prompt"))
{
j=br.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd.exe /c start");
}
System.out.println("Command me: ");
if(k.equals("run notepad"))
{
k=br.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("notepd.exe");
}
System.out.println("Command me: ");
if(l.equals("run paint"))
{
l=br.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("mspaint.exe");
}
System.out.println("Command me: ");
if(m.equals("open facebook"))
{
m=br.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd.exe /c start www.facebook.com");
}
System.out.println("Command me: ");
if(n.equals("open google"))
{
n=br.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd.exe /c start www.google.com");
}
System.out.println("Command me: ");
if(o.equals("open youtube"))
{
o=br.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd.exe /c start www.youtube.com");
}
System.out.println("Command me: ");
if(a.equals("open yahoo"))
{
a=br.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd.exe /c start www.yahoo.com");
}
System.out.println("Command me: ");
if(q.equals("open omegle"))
{
q=br.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("cmd.exe /c start www.omegle.com");
}
}
catch(Exception e)
{
System.out.print(e);
}
}
}
4

3 回答 3

2
String i=null,j=null,k=null,l=null,m=null,n=null,o=null,a=null,q=null;
try
{
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Command me: ");
     if(i.equals("jarvis you there?"))
         ...

变量inull,但您尝试取消引用它。履行

i=br.readLine();

在比较它的值之前。

于 2013-08-28T17:58:07.293 回答
0

您在i没有初始化的情况下进行比较

String i=null
if(i.equals("jarvis you there?"))
  ^^^^
Here there is NPE

你能做的最好的就是把常数放在等号的左边

if("jarvis you there?".equals(i))
{
   ...
于 2013-08-28T17:58:00.080 回答
0

代码按照您编写的顺序执行。如果要将用户输入与 an 中的某个值进行if比较,则必须在比较之前阅读它:

System.out.println("Command me: ");
i = br.readLine();
if(i.equals("jarvis you there?"))
{
    System.out.println("at your service sir");
}

这仍然会给你一个空指针异常,因为readLine()它可以返回 null。防止这种情况的最简单方法是切换equals. 文字字符串是对象,因此它们具有类似equals但不能为空的方法。

if("jarvis you there?".equals(i))

接下来你会发现你的程序读取一行并检查它是否只是第一个命令,读取下一行并检查它是否是第二个命令,依此类推。要获取"run paint"命令,您必须将其作为第 4 行输入。

您应该将同一行与每个可能的命令进行比较。例如通过使用if .. else if ... else if ... else

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (;;) {
    System.out.println("Command me: ");
    String line = br.readLine();
    if ("foo".equals(line)) {
        // do foo
    } else if ("bar".equals(line)) {
        // do bar
    } else if ("baz".equals(line)) {
        // do baz
    } else {
        System.out.println("Unknown Command. Bye.");
        break;
    }
}

或者在 Java 7 中switch

    switch (line) {
        case "foo":
            // do foo
            break;
        case "bar":
            // do foo
            break;
        //...
        default: 
            // } else {
            break;
    }

或者花哨的面向对象风格:http: //ideone.com/d6pf7T

于 2013-08-28T18:56:43.823 回答