Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Practicing Java: Null Pointer Exception

    I am building a calculator. I am not sure why I am getting a null pointer exception in my while loop.

    Code:
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class internalStructure {
        
        public static String[] input;
        public static float output;
        public static int place = 0;
        
        public static void main(String[] args) {
        
            System.out.print("Enter an equation: ");
            Scanner scan = new Scanner(System.in);
            while(scan.hasNext()){
                input[place] = scan.next();
                place++;
            }
            
            
    
            System.out.print(Arrays.toString(input));
    }
    }

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,822
    Thanks
    42
    Thanked 199 Times in 198 Posts
    because input is null. It has never been initialized.
    Code:
    public static String[] input = new String[somenumber];
    Why are you making them all static?

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •