0

EDIT: I understood what was wrong. I initialized adrsarr[4] before trying to put in values, then the problem was solved. Please close this thread.

package personal;

public class TestAddress {

    public void buildArray(Address[] adrsarr) {

        adrsarr[0] = new Address("B402", "3", "42", "Behind Sector 9",
                "New Delhi", "Delhi", "Delhi", "India", "232113");
        adrsarr[1] = new Address("B1", "2", "61", "Bomb Road 2", "Mumbai",
                "Mumbai", "Maharashtra", "India", "1213");
        adrsarr[2] = new Address("AH2", "325", "98", "BPGC", "Goa", "Goa",
                "Goa", "India", "403726");
        adrsarr[3] = new Address("a222", "2", "81", "Sector market",
                "New Delhi", "Delhi", "Delhi", "India", "11a001");
    }

    public void splitAddress(String addr , Address[] adrsarr) {

        String delim = ",";
        String[] address_split = addr.split(delim);
        // flatno, doorno, street, landmark, city, district, state,country, pin;
        adrsarr[4].setFlatno(address_split[0]);
        adrsarr[4].setDoorno(address_split[1]);
        adrsarr[4].setStreet(address_split[2]);
        adrsarr[4].setLandmark(address_split[3]);
        adrsarr[4].setCity(address_split[4]);
        adrsarr[4].setDistrict(address_split[5]);
        adrsarr[4].setState(address_split[6]);
        adrsarr[4].setCountry(address_split[7]);
        adrsarr[4].setPin(address_split[8]);

    }

    public void testingState(Address[] adrsarr) {

        for (int i = 0; i < 5; i++) {
            if (adrsarr[i].getState().equals("Goa")) {
                System.out.println("\n_____________Address" + (i + 1)
                        + "________________");
                System.out.println(adrsarr[i].getFlatno() + ","
                        + adrsarr[i].getDoorno() + "," + adrsarr[i].getStreet()
                        + "," + adrsarr[i].getLandmark() + ","
                        + adrsarr[i].getCity() + "," + adrsarr[i].getDistrict()
                        + "," + adrsarr[i].getState() + ","
                        + adrsarr[i].getCountry() + "," + adrsarr[i].getPin());
            }
        }
    }

    public void validatingPin(Address[] adrsarr) {

        for (int i = 0; i < 5; i++) {
            if (adrsarr[i].getPin().length() >= 6) {
                for (int j = 0; j < adrsarr[i].getPin().length(); j++) {
                    if (adrsarr[i].getPin().charAt(j) >= 48
                            && adrsarr[i].getPin().charAt(j) <= 57) {
                        continue;
                    } else {
                        System.out
                                .println(" \n SORRY WRONG PIN (composition) FOR ADDRESS"
                                        + (i + 1));
                        break;

                    }
                }
            } else {
                System.out.println(" \n SORRY WRONG PIN (length) FOR ADDRESS"
                        + (i + 1));
            }

        }
    }

    // MAIN

    public static void main(String[] args) {
        Address[] adrsarr = new Address[5];
        TestAddress ta1 = new TestAddress();
        ta1.buildArray(adrsarr);
        String test_addr = "#3A,1-164,Ist Line,Farmagudi,Ponda,Goa,403405";
        ta1.splitAddress(test_addr, adrsarr);
        ta1.testingState(adrsarr);
        ta1.validatingPin(adrsarr);
    }

}

So I have a TestAddress class. Through main, I'm first building an Address[] array, then I'm splitting a string into it's componenets and storing it into the respective appropriate Address variables. Then I'm doing some checks on the pin code and state. Simple enough.

As you can see, in main(), I've created an Address[] array which I'm passing to buildArray, which initializes the 4 objects adrsarr[0],adrsarr[1],adrsarr[2],adrsarr[3].

Then a test string and the adrsarr is passed to splitAddress which attempts to split the test string and put them into adrsarr[4]. This is where I'm getting the error:

Exception in thread "main" java.lang.NullPointerException
    at personal.TestAddress.splitAddress(TestAddress.java:22)
    at personal.TestAddress.main(TestAddress.java:81)

22th line is this :adrsarr[4].setFlatno(address_split[0]);

Now I can't see why this would raise a null pointer exception. I've tried to print address_split[] array before this line and it does contain the required strings.

Any help??

Edited by cyanide911

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by jalpesh_007
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.