2015-12-02 43 views
0

我正在做一個swing應用程序,它在Windows上工作「很好」,但它在(*)行發送錯誤。當我嘗試在Linux上運行它時發生錯誤(在Windows上正常運行)

public static void main(String[] args) throws ParseException, java.lang.InstantiationException { 

    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 




    /* Create and display the dialog */ 
    java.awt.EventQueue.invokeLater(() -> { 
     MainView dialog = null; 
     try { 
      dialog = new MainView(new javax.swing.JFrame(), true); 
     } catch (ParseException | SQLException | ClassNotFoundException ex) { 
      Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex); 
     } 

     dialog.setVisible(true); /// (*) This line throws the error 
    }); 
} 

我已經嘗試將代碼更改爲netbeans建議的內容,並且它一直顯示相同的錯誤。

這是NetBeans改變:

java.awt.EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      MainView dialog = null; 
      try { 
       dialog = new MainView(new javax.swing.JFrame(), true); 
      } catch (ParseException | SQLException | ClassNotFoundException ex) { 
       Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex); 
      } 

      dialog.setVisible(true); /// (*) This line throws the error 
     } 
    }); 

以下是錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
     at view.MainView.lambda$main$0(MainView.java:2842) 
     at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) 
     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) 
     at java.awt.EventQueue.access$500(EventQueue.java:97) 
     at java.awt.EventQueue$3.run(EventQueue.java:709) 
     at java.awt.EventQueue$3.run(EventQueue.java:703) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) 
+1

可能重複[什麼是零點r例外,我該如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –

+2

Did你檢查記錄器?也許它確實碰到了catch塊。你的對話框變量似乎仍爲空。 – SomeJavaGuy

+0

將dialog.setVisible(true)移動到新的Main下的try塊中...它不會打開,但至少沒有nullpointer ....:),然後檢查日誌... –

回答

3

爲什麼你想在對話框中設置爲可見如果在創建對話框失敗?

try { 
    dialog = new MainView(new javax.swing.JFrame(), true); 
    dialog.setVisible(true); // <--- move it here 
} catch (ParseException | SQLException | ClassNotFoundException ex) { 
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex); 
} 

此外

你應該在阻止你不必編寫極其醜陋線,如

} catch (ClassNotFoundException ex) { 
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
} catch (IllegalAccessException ex) { 
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
} catch (javax.swing.UnsupportedLookAndFeelException ex) { 
    java.util.logging.Logger.getLogger(MainView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
} 

,並將其簡化爲

} catch (ClassNotFoundException ex) { 
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex); 
} catch (IllegalAccessException ex) { 
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex); 
} catch (javax.swing.UnsupportedLookAndFeelException ex) { 
    Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex); 
} 
的方式使用進口( import java.util.logging.*
相關問題