-1

このコードで私を助けてください、私は実行時例外エラー 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);
}
}
}
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?"))
         ...

変数iはありますがnull、逆参照しようとしています。実行

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 に答える