2013-08-28 3 views
-1

이 코드로 나를 도와주세요, java.lang.nullpointerexception plz 런타임 오류 예외가 발생했습니다.이 문제를 해결하고 데스크탑에서 Java 프로그램을 사용하여 모든 파일에 액세스하는 방법을 알려주십시오. , 나는 바탕 화면이나 다른 folde에서 어떤 이미지 나 텍스트 파일에 액세스하고 싶은데 어떤 코드를 써야 하나? .FYI 나는 멍청하다. 동일의 당신은 초기화하지 않고 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); 
} 
} 
} 

답변

0

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

당신이 할 수있는 베스트 왼쪽에 일정하게 배치하는 것입니다

if("jarvis you there?".equals(i)) 
{ 
    ... 
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(); 

0

코드는 작성한 순서대로 실행됩니다. 당신이 if 내부에 어떤 값으로 사용자 입력을 비교하려는 경우에 당신은 당신이 그것을 비교하기 전에 읽어야 : readLine()는 null을 반환 할 수 있기 때문에

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

여전히 당신에게 널 포인터 예외를 줄 수있다. 이를 방지하는 가장 쉬운 방법은 equals의면을 전환하는 것입니다. 리터럴 문자열은 객체이므로 equals과 같은 메서드가 있지만 null 일 수 없습니다. 당신은 발견 할 것이다

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

다음 점은 첫 번째 명령은 경우 프로그램이 다음 라인을 읽고 등등 두 번째 명령하고 있는지 확인, 라인 및 검사를 읽는 것입니다. "run paint" 명령을 얻으려면 4 행으로 입력해야합니다.

가능한 모든 명령과 동일한 줄을 비교해야합니다. http://ideone.com/d6pf7T

: switch

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

또는 공상 객체 지향 스타일의 자바 7

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

또는 if .. else if ... else if ... else을 사용하여 예를 들어,

관련 문제