2011-06-20 59 views
-5

为了运行我的Java程序使用netbeans和im得到这个错误,任何建议?运行Java程序的错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.ImageIcon.<init>(ImageIcon.java:181) 
    at MainForm.addComponentsToPane(MainForm.java:28) 
    at MainForm.createAndShowGUI(MainForm.java:112) 
    at MainForm.access$000(MainForm.java:15) 
    at MainForm$4.run(MainForm.java:125) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641) 
    at java.awt.EventQueue.access$000(EventQueue.java:84) 
    at java.awt.EventQueue$1.run(EventQueue.java:602) 
    at java.awt.EventQueue$1.run(EventQueue.java:600) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:611) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 

这是代码:

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 




public class MainForm { 

    public static void addComponentsToPane(final JFrame frame, Container pane) { 
     Color colorGreen = new Color(0, 100, 0); 
     Color colorBrown = new Color(150, 100, 0); 
     //Color colorBlue = new Color (0, 0, 150); 
     //Font font = new Font("Verdana", Font.BOLD, 12); 

     pane.setLayout(null); 
     pane.setBackground(new Color (255, 255, 170)); 

     //add image and necessary labels on top left corner 
     //SHA Image and label 
     ImageIcon image = new ImageIcon(MainForm.class.getResource("SHA_logo.gif"));  
     JLabel label = new JLabel("Office of Traffic & Safety", image, JLabel.LEFT); 
     label.setVerticalTextPosition(JLabel.BOTTOM); 
     label.setHorizontalTextPosition(JLabel.CENTER); 
     label.setFont(new Font("Times New Roman", Font.PLAIN, 11)); 
     pane.add(label); 
     label.setBounds(50, 10, 130, 100); 
     label.setBorder(null); 

     //university of maryland image and label\\\ 
     image = new ImageIcon(MainForm.class.getResource("maryland_flag_round.gif")); 
     label = new JLabel("Univ. of Maryland", image, JLabel.LEFT); 
     label.setVerticalTextPosition(JLabel.BOTTOM); 
     label.setHorizontalTextPosition(JLabel.CENTER); 
     label.setFont(new Font("Times New Roman", Font.PLAIN, 11)); 
     pane.add(label); 
     label.setBounds(190, 10, 130, 90);  
     label.setBorder(null); 

     //critical lane label 
     label = new JLabel("Critical Lane Volume"); 
     label.setFont(new Font("Arial Narrow", Font.BOLD, 30)); 
     label.setForeground(colorGreen); 
     pane.add(label); 
     label.setBounds(50, 90, 250, 50); 
     label.setBorder(null); 

     label = new JLabel("<html>Please choose the analysis type:</html>"); 
     label.setFont(new Font("Arial", Font.BOLD, 18)); 
     label.setForeground(colorBrown); 
     pane.add(label); 
     label.setBounds(25, 130, 300, 70); 
     label.setBorder(null); 

     //back and exit buttons 
     JButton button1 = new JButton("Interchange"); 
     JButton button2 = new JButton ("Intersection"); 
     JButton button3 = new JButton ("Exit"); 
     pane.add(button1); 
     pane.add(button2); 
     pane.add(button3); 
     button1.setBounds(75, 200, 200, 50); 
     button2.setBounds(75, 270, 200, 50); 
     button3.setBounds(75, 340, 200, 50); 

     //add attap label at bottom left 
     image = new ImageIcon(MainForm.class.getResource("attap_logo.gif"));  
     label = new JLabel(image); 
     pane.add(label); 
     label.setBounds(30, 380, 270, 90);  
     label.setBorder(null); 

     button1.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0) { 
       frame.setVisible(false); 
       frame.dispose(); 
       InterchangeLoad.main(null); 
      } 
     }); 

     button2.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0) { 
       frame.setVisible(false); 
       frame.dispose(); 
       MultipleIntersectionLoad.main(null); 
      } 
     }); 

     button3.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0) { 
       frame.setVisible(false); 
       frame.dispose(); 
      } 
     }); 

    } 


    private static void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("Critical Lane Volume"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Set up the content pane. 
     addComponentsToPane(frame, frame.getContentPane()); 

     frame.setSize(350, 500); 
     frame.setResizable(false); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 

     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 

    } 

} 
+3

你可以发布代码吗? – insumity

回答

5

它看起来像你正在传递到您的ImageIcon构造函数的参数一个(或多个)为空。

EDIT

该异常是在这两条线中的一个发生:

ImageIcon image = new ImageIcon(MainForm.class.getResource("SHA_logo.gif"));  

image = new ImageIcon(MainForm.class.getResource("maryland_flag_round.gif")); 

在任一情况下,问题是相同的:所述资源没有被发现,所以null正在由getResource()返回。你为什么不使用new ImageIcon(String filename)?我甚至不确定getResource()在做什么。您的代码

其他一些快速的意见(建议):

pane.setLayout(null);是不是一个好主意,我甚至不能确定你可以摆脱的LayoutManager的那样。

对不同对象多次重复使用相同的引用(imagelabel)是错误的编码风格。你不会因为这样做而保存任何内存,而且代码的意义不大。

使用更多描述性名称! button1应该可能被称为'interchangeButton'或沿着这些线。对于这样一小段代码,没有人会迷路,但如果你从事大型项目的工作,一个好的命名方案至关重要。

+3

MainForm.java的第28行 – Snicolas

+0

这里真正的问题是为什么一个null参数被传递给构造函数......这意味着其他的代码可能是错误的。 @ user638234,发布你的代码! – Zarjio

+0

当没有找到图像文件时可能传递一个空值? –