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

Thread: add new element in array

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default add new element in array

    i need help urgently. i try to add new array element into my array which is cusName[20]. the process is that if the name already exist it wont add otherwise it will add to array cusName[20]. however i got error message java.lang.NullPointerException null. Please help me with the following code


    import java.util.*;
    public class AddCustomer
    {
    private String[] cusName;
    private int index;

    public AddCustomer()
    {
    index = 5;
    cusName = new String [20];

    cusName[0]= "Allen";
    cusName[1]= "Lisa";
    cusName[2]= "Jessica";
    cusName[3]= "Nicole";
    cusName[4]= "Ken";
    }

    public void addCustomer()
    {
    Scanner in = new Scanner (System.in);
    System.out.println("Input Customer Name");
    String name=in.next();

    int k, place, index;
    place= -1;
    for (k=0; k<cusName.length; k++)
    {
    if (cusName[k].equalsIgnoreCase(name))
    {
    System.out.println("Sorrry customer name " + name +" already exists");
    }

    }
    if (place == -1)
    {
    for (index=0; index<cusName.length; index++)
    {
    cusName[index]=name;

    }
    //System.out.println(cusName[index]);
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: add new element in array

    i got error message java.lang.NullPointerException null
    Please post the full text of the error message. Your edited version leaves out the source line number.
    Look at the error message, find the line number where the error occurs and then look at that line in the source and find what variable on that line has a null value. Backtrack in your code to find why that variable does not have a valid value.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: add new element in array

    You can basically also do as well

    /* only for testing */

    public class main{

    public static void main (String[] args){

    int array[]= {2, 5, 7, 8, 9}

    System.out.println(array[2]);


    }

    }

  4. #4
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: add new element in array

    Another tip is to look at each controlling variable in turn, and check where it is being initialized, changed and used.

    Do that with k, place and index, for example.

    ;-)

Similar Threads

  1. array element declaring problem
    By jack_nutt in forum Collections and Generics
    Replies: 11
    Last Post: August 1st, 2011, 06:29 PM
  2. URGENT!!!! help with array element comparing
    By Neo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 3rd, 2011, 07:52 AM
  3. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 02:26 PM
  4. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM
  5. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 08:04 AM