Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请在运行程序时帮助获取java.lang.nullpointerexception_Java_Windows_Cmd - Fatal编程技术网

请在运行程序时帮助获取java.lang.nullpointerexception

请在运行程序时帮助获取java.lang.nullpointerexception,java,windows,cmd,Java,Windows,Cmd,帮我解决这个代码,我得到运行时异常错误java.lang.nullpointerexception plz帮助我解决这个问题,以及如何使用java程序访问桌面上的任何文件, 就像我想访问桌面或任何其他文件夹中的任何图像或文本文件一样,我应该编写什么代码。仅供参考努布 import java.io.*; class jarvis { public static void main(String args[]) { String i=null,j=null,k=null,l=null,m=null,

帮我解决这个代码,我得到运行时异常错误java.lang.nullpointerexception plz帮助我解决这个问题,以及如何使用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);
}
}
}

您正在比较
i
,但未对其进行初始化

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);
}
}
}
String i=null
if(i.equals("jarvis you there?"))
  ^^^^
Here there is NPE
你们能做的最好的事情就是把常数放在等式的左边

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);
}
}
}
if("jarvis you there?".equals(i))
{
   ...
变量
i
null
,但您尝试取消引用它。表演

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);
}
}
}
i=br.readLine();

在比较其值之前。

代码按照您编写它的顺序执行。如果要将用户输入与
If
中的某个值进行比较,则必须先读取该值,然后再进行比较:

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);
}
}
}
System.out.println("Command me: ");
i = br.readLine();
if(i.equals("jarvis you there?"))
{
    System.out.println("at your service sir");
}
由于
readLine()
可以返回null,因此仍然可能会出现null指针异常。防止这种情况发生的最简单方法是切换
equals
的两侧。文字字符串是对象,因此它们具有类似于
equals
的方法,但它们不能为null

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);
}
}
}
if("jarvis you there?".equals(i))
接下来您会发现,您的程序读取一行并检查它是否仅是第一个命令,读取下一行并检查它是否是第二个命令,依此类推。要获取
“run paint”
命令,必须将其作为第4行输入

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);
}
}
}
您应该将同一行与每个可能的命令进行比较。例如,使用
if。。否则如果。。。否则如果。。。其他

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);
}
}
}
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中使用
开关

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);
}
}
}
    switch (line) {
        case "foo":
            // do foo
            break;
        case "bar":
            // do foo
            break;
        //...
        default: 
            // } else {
            break;
    }
或者以奇特的面向对象风格:

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);
}
}
}