0

I am a beginner in Java development. Want to add image as background of a JPanel. I've tried many solutions from last 4 hours and ended up with this error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(ImageIO.java:1348) at schedule.loginPrintTable$1.run(loginPrintTable.java:107) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)

Please note that I don't have main() in this file and I am not suppose to use main() for adding image as background.

class loginPrintTable extends JLabel{


    JFrame tableframe;

    private table[][][] ttable;
    DefaultTableModel model = new DefaultTableModel();
    JTable Table;

    int stgrp;
    int i;

    JPanel south;
    inputdata input;

    public loginPrintTable(int nostgrp, inputdata input1) {
        // ttable = t;
        tableframe = new JFrame();
        stgrp = nostgrp;
        south = new JPanel();
    }



    void print() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                tableframe.setTitle("CLASS SCHEDULING");
                tableframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                tableframe.setSize(800, 600);

                tableframe.setLayout(new GridBagLayout());

                GridBagConstraints c = new GridBagConstraints();
                c.weightx = 0;
                c.gridx = 1000;
                c.gridy = 50;
                c.fill = GridBagConstraints.CENTER;
                int j;

                JPanel panel;
                JLabel label1, label2;
                final JTextField text1, text2;
                label1 = new JLabel();

                label1.setText("Username:");
                text1 = new JTextField(15);
                JButton next = new JButton("Login");
                JButton next1 = new JButton("Cancel");

                label2 = new JLabel();
                label2.setText("Password:");
                text2 = new JPasswordField(15);
                panel = new JPanel(new GridLayout(3, 1));

                panel.add(label1);
                panel.add(text1);
                panel.add(label2);
                panel.add(text2);
                panel.add(next);
                panel.add(next1);



                        try {
                            InputStream imageStream = this.getClass().getResourceAsStream("asset/login.jpeg");
                            BufferedImage image;

                            image = ImageIO.read(imageStream);

                            JLabel picLabel = new JLabel(new ImageIcon(image));

                            panel.add(picLabel);

                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                JPanel panel2;
                JLabel label3;

                label3 = new JLabel();

                label3.setText("Progressing...Please wait....");

                panel2 = new JPanel(new GridLayout(3, 1));
                panel2.add(label3);

                JScrollPane scroll = new JScrollPane(Table);
                // panel=new loginPanel(ttable,stgrp,input);
                 JPanel south = new JPanel();
                 south.setLayout(new GridLayout(1, 10));
                 for (j = 0; j < 2; j++) {
                 south.add(new JLabel(" "));
                 }
                 south.add(next);

                 south.add(next1);

                tableframe.add(panel);

                 c.gridx = 0;
                 c.gridy = 10;
                 tableframe.add(south, c);
                tableframe.setVisible(true);


            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Where exactly are you getting this exception from? Can you share a stacktrace? – Mureinik Apr 02 '16 at 10:58
  • Getting exception at Image IO read – Chetan Potdar Apr 02 '16 at 11:01
  • Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(ImageIO.java:1348) at schedule.loginPrintTable$1.run(loginPrintTable.java:107) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) – Chetan Potdar Apr 02 '16 at 11:02
  • did you check what is the value of `imageStream`? – Deendayal Garg Apr 02 '16 at 11:04
  • 1
    so, ```imageStream``` is null? Meaning something goes wrong in the call to ```getResourceAsStream```. Doc says: _"getResourceAsStream: This method returns an input stream for reading the resource, or null if the resource could not be found."_ – Jorn Vernee Apr 02 '16 at 11:06
  • yes...imageStream is null.. I tried placing this image at other loacations as well but still the same. – Chetan Potdar Apr 02 '16 at 11:09
  • Any other solution by which i can add image as background? – Chetan Potdar Apr 02 '16 at 11:18
  • Some examples are cited [here](http://stackoverflow.com/tags/embedded-resource/info). – trashgod Apr 02 '16 at 11:34

0 Answers0