0

Apologies for bothering you all with this relatively simple question, but I keep on running into a null pointer exception when calling replaceAll("\.","") and I was wondering why...Undoubtedly it's something simple that I'm just not getting.

Brief Summary of IPAddress: The first class mentioned will be passed a String value from IPAddressTester (mentioned afterwards) which will be stored in class IPAddress as ipString. The string itself will be of form ###.###.###.##. My eventual goal will be to strip away the periods of this string, combine the resulting tokens, and convert to an integer for later manipulation (see hashCode method). Obviously this class is incomplete and it might very well have other problems/issues, but I am currently focusing merely on the null pointer error resulting from replaceAll.

Brief Summary of IPAddressTest: Exactly as its name implies, this tests IPAddress. Basically a "driver".

package ipResolver;

import java.util.Arrays;
import java.util.StringTokenizer; 
import java.util.Scanner;

public class IPAddress implements Comparable<IPAddress>{

     private String ipString;

     private int ipInt;
     private int ipHash;


     //Constructor
     public IPAddress(String ip){
         ipString = ip;
     }


     //--------------------------------Begin Error Region
     String tmpString = ipString.replaceAll("\\.","");
     //--------------------------------End Error Region        

     public int hashCode(int ipToBeHashed){
         return ipHash;
     }

     public boolean equals(Object o){
         return ipString.equals(String.valueOf(o));
     }

     public int compareTo(IPAddress IP){
         return ipInt - IP.ipInt;
     }

     public String toString()
     {
         return ipString;
     }

     public void gettmpString(){
         System.out.println(tmpString);
     }
}

IPAddressTest Class

package test_code;
import java.util.Arrays;

import ipResolver.IPAddress;

public class IPAddressTest {
    public static void main(String args[]){
        String testString = "130.191.208.70";
        IPAddress ipAdd = new IPAddress(testString);
        System.out.println(ipAdd);

        ipAdd.gettmpString();

    }
}

Again, my sincere apologies for bothering you all with this, but I'm guessing this question will probably have a simple one or two sentence answer (which will hopefully not ahem be too insulting ;-)).

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user1309459
  • 1
  • 1
  • 1

3 Answers3

2
String tmpString = ipString.replaceAll("\\.","");

That code should be in the constructor, after you have actually assigned a value to ipString (because otherwise it is still null).

public class IPAddress implements Comparable<IPAddress>{

     private final String ipString;

     private final String tmpString;

     //Constructor
     public IPAddress(String ip){
         ipString = ip;
         tmpString = ipString.replaceAll("\\.","");
     }

}

Or maybe you don't want to store the tmpString at all, and calculate it on use:

public String getTmpString(){
    return ipString.replaceAll("\\.","");
}
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

You're initializing

   String tmpString 

with a null ipString. IpString doesn't get set until the constructor has been called, however tmpString gets initialized BEFORE the constructor is called due to its placement in the class initializer.

Initialize tmpString in the constructor to resolve the issue.

Jayan
  • 18,003
  • 15
  • 89
  • 143
Tommy B
  • 187
  • 2
  • 8
0

Initial order.

when you new the IPAddress, the IPAddress.ipString is null.

The initial process!

1,IPAddress ipAdd;

2,ipString=null;

3,String tmpString = ipString.replaceAll("\.",""); //throw Exception

4,IPAddress ipAdd=new IPaddress(testString);

5,ipString="130.191.208.70";