2012-11-16 15 views
1

임의의 숫자 배열을 기반으로 그림을 그리려합니다. 지금까지 배경색 (흰색)이 나오기 때문에 부분적으로 성공했습니다.그림을 그릴 때 NullPointerException이 발생했습니다

그러나 그림은 그려지지 않습니다. 대신 내가 본 모든 것은 흰색 배경과이 오류입니다 :

PixelAvatarGen 
XphnX 
Black and white or 8-bit color? 
0 = B&W, 1 = 8bit 
1 
Print name to make into avatar. 
2 
5:2:7:0:3:6:7:0:6:7:7:3:1:4:0:6:5:1:7:4:0:0:5:6:6:3:0:2:3:7:2:4:7:5:4:2:3:6:0:1:4:1:6:4:1:1:1:5:7:5:1:5:4:0:4:5:1:5:6:0:5:4:6:0:0:6:0:0:0:6:7:5:2:6:4:0:1:1:1:7:0:2:1:4:0:2:3:3:3:3:2:6:6:2:6:4:2:7:5:4:7:3:3:3:1:5:1:6:1:3:4:6:0:1:1:2:5:7:4:1:0:2:4:4:4:6:3:7:7:1:0:6:3:6:3:7:6:5:2:5:5:3:5:3:5:4:4:7:0:5:7:4:2:4:5:7:1:0:3:2:7:7:2:3:5:5:2:7:5:6:6:7:4:3:0:7:4:7:1:0:0:7:4:4:3:7:6:0:4:2:7:6:7:3:2:4:2:7:3:0:5:6:6:7:7:2:2:0:2:5:7:6:5:1:0:6:1:5:2:0:7:0:7:0:4:1:0:3:4:3:7:7:4:4:2:7:0:7:4:7:1:6:1:4:4:2:4:7:3:0:4:4:6:6:3:5: 
Drawing11 
Exception in thread "AWT-EventQueue-0" Drawing11 
java.lang.NullPointerException 
at xphnx.pixelgen.Screen.paintComponent(Screen.java:25) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JLayeredPane.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) 
at javax.swing.RepaintManager.paint(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) 
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) 
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) 
at java.awt.Container.paint(Unknown Source) 
at java.awt.Window.paint(Unknown Source) 
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.access$700(Unknown Source) 
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) 
at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$200(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at xphnx.pixelgen.Screen.paintComponent(Screen.java:25) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) 
at javax.swing.RepaintManager.paint(Unknown Source) 
at javax.swing.JComponent._paintImmediately(Unknown Source) 
at javax.swing.JComponent.paintImmediately(Unknown Source) 
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.access$700(Unknown Source) 
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) 
at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$200(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
at java.awt.EventDispatchThread.run(Unknown Source) 

이것은 프로그램의 완전한 출력물입니다. 0 이후의 "1"= B & W 및 "Print name to ..."다음의 "2"는 사용자 입력입니다. 여기

는 Screen.java이다 필요한 경우

public class Screen extends JPanel { 

    String[] pixels; 
    Color[] proxlol; 

    public Screen(String[] in1, Color[] in2) { 
    in1 = pixels; 
    in2 = proxlol; 
    } 

    public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    this.setBackground(Color.WHITE); 
    int row = 1, column = 1, row10, column10; 
    for(int i = 0; i != 256; i++) { 
     System.out.println("Drawing" + row + column); 
     g.setColor(proxlol[i]); 
     if(i == 17 || 
      i == 33 || 
      i == 49 || 
      i == 65 || 
      i == 81 || 
      i == 97 || 
      i == 113 || 
      i == 129 || 
      i == 145 || 
      i == 161 || 
      i == 177 || 
      i == 193 || 
      i == 209 || 
      i == 225 || 
      i == 241 
     ){ 
     row++; 
     column = 1; 
     } 
     row10 = row*10; 
     column10 = column*10; 
     g.fillRect(row10, column10, 10, 10); 
     System.out.println("Done drawing" + row + column); 
     column++;  
    } 
    } 
} 

, Main.javaArranger.java는 페이스트 빈에있다.

전 스윙 경험이 거의 없습니다. 무슨 일이야?

+1

다음 번에 *** 관련 *** 코드 *** 부분을 직접 질문에 포함하십시오. 예외 스택 추적 (stacktrace)은 문제가 실제로 발생한 위치에 대한 올바른 방향을 알려줍니다. – Perception

+0

물고기가 거기 밖으로 무엇입니까? –

+0

['PiRaster'] (http://stackoverflow.com/a/7298492/230513)도 참조하십시오. – trashgod

답변

5

클래스 변수 proxlol에 값을 할당하지 마십시오.

public class Screen extends JPanel { 
    String[] pixels; 
    Color[] proxlol; 

    public Screen(String[] in1, Color[] in2) { 
     in1 = pixels; 
     in2 = proxlol; 
    } 
} 

당신은 (효과 없음으로) 지정 입력 매개 변수 in2에 초기화되지 않은 클래스 변수. 그래서, 당신의 페인트 방법에서 당신이 NPE를 얻는 Colors 배열을 참조 할 때.

+0

기본적으로 나는 변수를 거꾸로 가지고 있습니다. 감사. – JXPheonix

관련 문제