Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: What's wrong with my code

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code

    package Enrollment;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class Login extends JFrame{
     
        public String name;
        JTextField txtUsername;
     
        public Login(){
     
            setLayout(new FlowLayout());
            txtUsername = new JTextField(" ", 10);
            add(txtUsername);
     
            JButton btnLogin = new JButton("Login");
            add(btnLogin);
     
            btnLogin.addActionListener(new ButtonListener());
        }
     
        private class ButtonListener implements ActionListener{
            @Override
              public void actionPerformed(ActionEvent e){
     
               if(name == txtUsername.getText()){
                    Student StudentRegistration = new Student();
                    StudentRegistration.run();
                    StudentRegistration.lblName.setText(name);
                    dispose();
               };
     
     
            }
     
        }
     
        public void run(){
            setTitle("Login");
            setBounds(200,200,350,200);
            setVisible(true);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    package Enrollment;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Student extends JFrame {
     
        Container con = null;
        JLabel picStudent;
        JLabel lblName;
     
        public Student(){
     
            con = getContentPane();
            con.setLayout(null);
     
            ImageIcon imh = new ImageIcon(getClass().getResource("Images/bg.jpg"));
            Icon studpic = new ImageIcon(getClass().getResource("Images/studpic.jpg"));
            setSize(imh.getIconWidth(), imh.getIconHeight());
     
             JPanel panel = new JPanel(){
                 @Override
                public void paintComponent(Graphics g) 
                {
                    Image imh = new ImageIcon("Images/bg.jpg").getImage();
                    Dimension size = new Dimension(imh.getWidth(null), imh.getHeight(null));
                    setPreferredSize(size);
                    setMinimumSize(size);
                    setMaximumSize(size);
                    setSize(size);
                    setLayout(null);
                    g.drawImage(imh, 0, 0, null);
                } 
             };
             con.add(panel);
             panel.setBounds(0, 0, imh.getIconWidth(), imh.getIconHeight());
             JPanel panelcontent = new JPanel(new FlowLayout());
             panelcontent.setBounds(0,0,200,200);
             panel.add(panelcontent);
             picStudent = new JLabel(studpic);
             lblName = new JLabel();
             panelcontent.add(picStudent);
             panelcontent.add(lblName);
    }
     
        public void run(){
     
             setTitle("Student Registration");
             setBounds(0,0,575,431);
             setVisible(true);
             setResizable(false);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        }
    }
    package Enrollment;
     
     
    public class Main {
        public static void main(String[] args){
     
            Login login = new Login();
            login.run();
     
     
     
     
        }
     
     
    }
    this is my error output
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
    at Enrollment.Student.<init>(Student.java:19)
    at Enrollment.Login$ButtonListener.actionPerformed(Lo gin.java:29)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What's wrong with my code

    At line 19 there is a variable or value returned by a method with a null value. Find the variable or method and backtrack to find out why its value is null.

    Does the image file exist at the location referred to by the code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code

    Yes, all of my image file is at the right location.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What's wrong with my code

    image file is at the right location.
    What does this method call return?
    getClass().getResource("Images/bg.jpg")
    If it returns null then the image is not in the correct location.
    Be sure to test the value for all images.
    Last edited by Norm; September 26th, 2012 at 09:11 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What is wrong with my code?
    By unleashed-my-freedom in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2012, 12:41 AM
  2. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM
  3. want wrong with my code
    By drum in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 09:06 AM
  4. What is Wrong with my code
    By xew123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:59 AM
  5. What's wrong with my code?
    By lindmando in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2011, 11:13 PM