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

Thread: How can i fix my null pointer exception error?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy How can i fix my null pointer exception error?

    This is my entire code.. what i do keep getting nullpointerexception error whenever i input 1 or 2 whenever the code is asking me to input the type of display to be shown..

    this is my error - "Exception in thread "main" java.lang.NullPointerException
    at dakilangtaoako.DakilangTaoAko.alphabetSorter(Dakil angTaoAko.java:195)
    at dakilangtaoako.DakilangTaoAko.main(DakilangTaoAko. java:91)
    Java Result: 1 "

    package dakilangtaoako;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.logging.Level;
    import java.util.logging.Logger;


    public class DakilangTaoAko {
    public static void main(String[] args){

    BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
    String mydata[][] = new String[5][3] ;
    String inttoword [] = new String [5];
    int wordtoint[] = new int [5];
    int mychoice = 0;
    String toreturn = "";

    for (int outside = 0; outside < 5; outside++){
    System.out.println("Entry no." + (outside + 1));
    for (int inside = 0; inside < 3; inside++){
    if(inside == 0){
    try {
    System.out.print("Name: ");
    mydata[outside][inside]= dataIn.readLine();
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.parse("OK"), null, ex);
    }
    }
    else if(inside == 1){
    try {
    System.out.print("ID: ");
    mydata[outside][inside]= dataIn.readLine();
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.parse("OK"), null, ex);
    }
    }
    else if(inside ==2){
    try {
    System.out.print("Subject: ");
    mydata[outside][inside]= dataIn.readLine();
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.parse("OK"), null, ex);
    }


    }
    }
    }
    for(int outside= 0; outside <5; outside++){
    for(int inside = 1; inside <= 3; inside++){
    if(inside ==0)
    inttoword[outside]=(mydata[outside][inside])+";"+(mydata[outside][inside]);
    }
    }
    for(int outside = 0; outside < 5; outside++){
    for(int inside = 1; inside <= 3; inside++){
    if(inside ==0)
    wordtoint[outside]=Integer.parseInt(mydata[outside][inside]);
    }
    }


    for(int i=0;i<5;i++){
    wordtoint[i]=Integer.parseInt(mydata[i][1]);
    }

    while(mychoice != 5){
    System.out.println("-------- DISPLAY OPTIONS --------");
    System.out.println("\t [1] Display Entries Alphabetically ");
    System.out.println("\t [2] Display Entries by Ascending ID number ");
    System.out.println("\t [3] Display Students Enrolled in Prog2 ");
    System.out.println("\t [4] Display Students Enrolled in CSO ");
    System.out.println("\t [5] Exit ");
    System.out.println();
    System.out.print("\t Choice:");
    try {
    mychoice = Integer.parseInt(dataIn.readLine());
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
    }

    switch (mychoice){



    case 1:
    System.out.println("----Entries In Alphabetical Order----");
    alphabetSorter(inttoword);
    for(int limiter = 0; limiter < 5; limiter++)
    System.out.println(inttoword[limiter]);


    System.out.print("Press [r] to Return to Main Menu...");
    try {
    toreturn = dataIn.readLine();
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
    }
    if("r".equals(toreturn))
    break;
    case 2:

    System.out.println(" ---- Entries by Ascending ID Number ----");
    for(int outside = 0; outside < 5; outside++){
    numSorter(wordtoint);
    for(int inside = 0; inside < 5; inside++){
    if(Integer.parseInt(mydata[inside][1])== wordtoint[outside])
    System.out.println(wordtoint[outside] + ";" + mydata[inside][0]);
    }
    }
    System.out.print("Press [r] to return to main Menu...");
    try {
    toreturn = dataIn.readLine();
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
    }
    if("r".equals(toreturn))
    break;

    case 3:
    System.out.println(" ---- Students Enrolled in Prog2 ----");
    String mustfind1 = "PROG2";
    for(int outside = 0; outside < 5; outside++){
    if(mydata[outside][2].equalsIgnoreCase(mustfind1)){
    System.out.println(mydata[outside][0]+";"+mydata[outside][1]);
    }
    }System.out.print("Press [r] to Return to Main Menu...");
    try {
    toreturn = dataIn.readLine();
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
    }
    if("r".equals(toreturn))
    break;

    case 4:
    System.out.println(" ---- Students Enrolled in CSO ---- ");
    String mustfind2 = "CSO";
    for(int outside = 0; outside < 5; outside++){
    if(mydata[outside][2].equalsIgnoreCase(mustfind2))
    System.out.println(mydata[outside][0]+";"+mydata[outside][1]);
    }
    System.out.print("Press [r] to Return to Main Menu...");
    try {
    toreturn = dataIn.readLine();
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
    }
    if("r".equals(toreturn))
    break;

    case 5:
    mychoice = 5;

    break;

    default:
    System.out.println("Sorry the Number is Not in the Choices!!");
    System.out.print("Press [r] to Return to Main Menu...");
    try {
    toreturn = dataIn.readLine();
    } catch (IOException ex) {
    Logger.getLogger(DakilangTaoAko.class.getName()).l og(Level.SEVERE, null, ex);
    }
    if("r".equals(toreturn))
    break;
    } }
    }
    public static void numSorter(int given[]){

    int temp;
    for(int outside = 0; outside < given.length-1; outside++)
    {
    for(int inside = outside+1 ; inside < given.length; inside++)
    {
    if(given[outside] > given[inside])
    {
    temp = given[outside];
    given[outside]= given[inside];
    given[inside]=temp;
    }
    }
    }
    }
    public static void alphabetSorter(String given[]){

    String temp;
    for(int outside = 0; outside < given.length -1; outside++)
    {
    for(int inside = outside +1; inside < given.length; inside++)
    {
    if(given[outside].compareToIgnoreCase(given[inside])>0)
    {
    temp= given[outside];
    given[outside]= given[inside];
    given[inside]=temp;
    }
    }
    }
    }

    }


  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: How can i fix my null pointer exception error?

    his is my error - "Exception in thread "main" java.lang.NullPointerException
    at dakilangtaoako.DakilangTaoAko.alphabetSorter(Dakil angTaoAko.java:195)
    Look at line 195 and find the variable with the null value. Then backtrack in the code to find why that variable does not have a valid value.

    Please edit your post and wrap your code with code tags:
    [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.

Similar Threads

  1. Null Pointer Exception. How to fix this problem?
    By carbLoading2012 in forum Exceptions
    Replies: 2
    Last Post: February 4th, 2013, 03:06 AM
  2. Null Pointer Exception error
    By Keitho55 in forum Exceptions
    Replies: 3
    Last Post: November 18th, 2011, 03:29 AM
  3. NULL POINTER EXCEPTION error
    By beefwithrice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:26 AM
  4. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM

Tags for this Thread