Jump to content

java help

F0xX
can anyone help me figure out why i have a NullPointerException? thanks

import java.io.*;

 

public class NameSearchProgram {

  public static final int MAX_NAMES = 90000;

  public static int         numNames;

  public static String[]    names = new String[MAX_NAMES]; 

  

  // BubbleSort algorithm to sort all names by their length

  public static void bubbleSortByLength() {

    // ... Complete this ..

    for(int p=0; p < names.length; p++){

      if(names[p]==null){

        names[p]="expceptionNotFound";

        

        String storeName;

        

        for (int i=0; i<(names.length); i++){

          for(int j=0; j<(names.length); j++){

            if(names.length() > names[i+1].length()){

              storeName = names;

              names = names[i+1];

              names[i+1] = storeName;

               }

        }

      }

      }

    }

    }

Lenovo Y50-70 Touch

I7-4700

GTX-860M 2gb

Link to comment
Share on other sites

Link to post
Share on other sites

 

-SNIP-

 

From the code given, you have absolutely no Strings in your names array. The code is looping to locations in the array which contain nothing.

Current PC: Origin Millennium- i7 5820K @4.0GHz | GTX 980Ti SLI | X99 Deluxe 

 

Link to comment
Share on other sites

Link to post
Share on other sites

When you do

  public static String[]    names = new String[MAX_NAMES]; 

You declare an array , but you do not instantiate its elements , which remain null.

 

You need to initialize the array elements.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

When you do

  public static String[]    names = new String[MAX_NAMES]; 

You declare an array , but you do not instantiate its elements , which remain null.

 

You need to initialize the array elements.

woops sorry later on in the code it takes the string array from a txt file

Lenovo Y50-70 Touch

I7-4700

GTX-860M 2gb

Link to comment
Share on other sites

Link to post
Share on other sites

You should learn how to debug Java programs. When you're starting out with a new language you are going to run into a lot of problems like the one you just hit. Knowing how to debug your application will make solving your issues a lot faster and easier. Take a look at this: http://www.vogella.com/tutorials/EclipseDebugging/article.html

 

But to help with your question:

Lets add some strings to your names array and walk through bubbleSortByLength().

Lets say names contains a single name "Linus".

public static void bubbleSortByLength() {    // ... Complete this ..    for (int p = 0; p < names.length; p++) {        if (names[p] == null) {            names[p] = "expceptionNotFound";            String storeName;            for (int i = 0; i < (names.length); i++) {                for (int j = 0; j < (names.length); j++) {                    if (names[i].length() > names[i + 1].length()) { // i = 0, names[0] = "Linus", names[0 + 1] = null. You are trying to get the length() of a null string. That's what is causing your NullPointerException                        storeName = names[i];                        names[i] = names[i + 1];                        names[i + 1] = storeName;                    }                }            }        }    }}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×