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 6 of 6

Thread: Exception in thread "main" java.lang.NullPointerException ! Need help ! Urgent ! Please !

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Exception in thread "main" java.lang.NullPointerException ! Need help ! Urgent ! Please !

    I rewiewed my code try to figure out why it thrown an exception . But i failed . This is for my assignment . Please help .
    The exception is :
    Exception in thread "main" java.lang.NullPointerException
    at week11demo1.Main.<init>(Main.java:54)
    at week11demo1.Main.main(Main.java:126)
    Java Result: 1


    Heres my codes :
    package week11demo1;

    <// Imports to cover our use of Swing and AWT.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     
    // Our main class will be a JFrame, and since we want
    // to handle button clicks, we implement ActionListener.
     
    public class Main extends JFrame implements ActionListener {
        private JFrame window=new JFrame("Welcome to PasswordEvaluator ");
        private static final int WIDTH = 500;
        private static final int HEIGHT = 250;
        // Text areas for information display and input.
        private JTextArea criteria;
        private JTextArea password;
        private JPanel center;
        private JPanel south;
        private JLabel reminder;
     
        // Buttons for user interaction.
        private JButton check=new JButton("Check");
        private JButton exit= new JButton("Exit");
     
     
     
        public Main() {
            window.setSize(WIDTH, HEIGHT);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            // Don't let user resize window.
            //this.setResizable(false);
            // Set the layout to a flow layout.
            window.setLayout(new BorderLayout());
     
            // Set up the text areas and add to form.
            criteria = new JTextArea(100,300);
            criteria.setText("Enter Your Password Below. And Click on the CHECK button to check your password strength The password should be at least six characters long. The password should contain at least one uppercase and at least one lowercaseletter. The password should have at least one digit.");
            //criteria.setLineWrap(true);
            window.add(criteria,BorderLayout.NORTH);
     
            center.setLayout(new FlowLayout());
            reminder.setText("Password:    ");
            password.setSize(50, 20);
            //password.setLineWrap(true);
            center.add(reminder,FlowLayout.LEFT);
            center.add(password,FlowLayout.RIGHT);
     
     
            // Set up and add the buttons.
     
            // Invoke actionPerformed for Main class.
            check.addActionListener(this);
     
     
            // Invoke actionPerformed for Main class.
            exit.addActionListener(this);
     
            south.setLayout(new FlowLayout());
            south.add(check);
            south.add(exit);
            window.setVisible(true);
     
        }
     
     
        private static String IsValid(String pass){
            int digitCount = 0;
            if(pass.length() >= 8)
    {    
        for (char c : pass.toUpperCase().toCharArray()) {
            if((int)c >= 48 && (int)c <= 57)
    {
    // This is a digit
        digitCount++;
    }
            else if((int)c >= 65 && (int)c <= 90)
    {
    // This is an alphabetical character
    }
            else
    {
    // Other character
     
        return "Invalid: unacceptable character found '" + c + "'";
    }
    }
        if(digitCount != 2)
    {
        return "Invalid: password must contain exactly 2 digits";
    }
    }
            else
    {
        return "Invalid: password must contain 8 characters at least";
    }
            return "Valid";
    }
         public void actionPerformed(ActionEvent e) {
             Object button = e.getSource();
            // Now test for which was clicked and act accordingly.
     
             if (button == exit) {       
                 System.exit(0);  // Get out of Dodge.
            }
             else if (button == check) {
                 String text = password.getText();
                 text = IsValid(text);
            }
             else {}    
         }
     
         public static void main (String[] args) {
             new Main();
         }
    }>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException ! Need help ! Urgent ! Please !

    Exception in thread "main" java.lang.NullPointerException
    at week11demo1.Main.<init>(Main.java:54)
    At line 54 there is a variable that has a null value when that line is executed. Look at line 54, find the variable with the null value and then backtrack in the code to see why the variable does not have a valid value.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException ! Need help ! Urgent ! Please !

    If it's very "urgent" I suggest a couple of things:
    • Go through the forum FAQ to see how to post well formatted code using code tags so that it's readable. If your problem needs a solution soon, there's no sense in your making it hard for us to read your posted code.
    • Show us which line is line 54
    • Please edit your original post with the advanced editor and remove all references to "urgent" and "ASAP" as these usually have the opposite effect to what you intend. Most of us are pretty egalitarian-minded and many bristle when someone comes along shouting in effect that "my question is more important than any one else's question" when in fact it isn't. All questions are equally important here, and we respect questioners who respect this fact.

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException ! Need help ! Urgent ! Please !

    sorry , this is my first post .
    54 is : center.setLayout(new FlowLayout());
    124 is : new Main();

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,923
    Thanks
    55
    Thanked 2,352 Times in 2,323 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException ! Need help ! Urgent ! Please !

    What value does the variable: center have?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException ! Need help ! Urgent ! Please !

    Quote Originally Posted by ryanlu View Post
    sorry , this is my first post .
    54 is : center.setLayout(new FlowLayout());
    124 is : new Main();
    Again, please advanced edit your original post and remove the urgency statements.

    Per line 54, only one variable can be null there, center. So search your code to see where you think you've created an object for this variable.

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By SoooConfused in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 10:06 PM
  2. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 03:23 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By linux.sys in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 22nd, 2012, 07:01 AM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. Replies: 1
    Last Post: August 31st, 2011, 11:48 AM