Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I having trouble figure out how to get rid off the java.lang.NullPointerExceptio

ID: 3538795 • Letter: I

Question

I having trouble figure out how to get rid off the java.lang.NullPointerException error

Help me correct my code (such as indexOF and contains method)


example: java.lang.NullPointerException
    at p903_LinkedList.SinglyLinkedList.indexOf(SinglyLinkedList.java:127)
    at p903_LinkedList.SinglyLinkedList.contains(SinglyLinkedList.java:88)



public class SinglyLinkedList<E> implements ILinkedList<E>{

    /** Node to keep track of the head (beginning of the list) */
    private SinglyNode<E> head;
    /** Logical size of the list */
    private int size;
   
   
    /** initializes*/
    public SinglyLinkedList(){
       
        this.head = null;
        size = 0;
    }
   
    /** Adds the parameter to the beginning of the list*/
    public void add(E data) {
   
        if (size == 0){
           
            //add into beginning of the list
            SinglyNode<E> cu = this.head;
            this.head = new SinglyNode<E>(data);
           
        }else{
           
            SinglyNode<E> cu = head;
            SinglyNode<E> current = null;
            while((current = cu.next) != null) //find the last Node
                cu = cu.next;
            cu.next = new SinglyNode<E>(data);

           
        }
       
            size++;
    }

    /**    
    * Inserts the parameter to the at the specified position in the list.
    * Note that it throws an Exception if the position is non-existent
    * */
   
    public void add(int pos, E data) throws Exception {

        if(data == null){                                //check if item is null
         
            throw new NullPointerException();

        }else if(pos < 0 || pos > size) {               //check if the index is small than 1 or not
                                                        //check if the index is exist or not  
            throw new Exception("No such index");
         
        }else if (pos == 0) {                        
         
            head = new SinglyNode<E>(data);
         
        }else{
         
            SinglyNode<E> current = head;                //Temporary Node
         
            for (int i = 0; i < pos - 1; i++) {
             
                current = current.next;
            }
            SinglyNode<E> present = new SinglyNode<E> (data);
            SinglyNode<E> next = current.next;
            current.next = present;
            present.next = next;
          
        }
            size++;
     
    }

    /**Returns true if the list contains the specified element. */
    public boolean contains(E data) {
       
            try {
               
                return indexOf(data) != -1;   //return data of index
               
            } catch (Exception e) {

                e.printStackTrace();
            }
            return false;
       
       
    }

    /**Gets the element at the specified position*/
    public E get(int pos) throws Exception {

       
        if (pos < 0 || pos >= size) {                //check if the index is out of bounds or not
            throw new Exception("out of bounds");
        }   
       
        SinglyNode<E> temp = head;               
       
        for (int i = 0; i < pos; i++) {                //loop through the list to find the index and return the index of data
            temp = temp.next;
        }
       
        return temp.data;
       
    }
   
    /**
    * Finds the index of the first occurrence of the specified element.
    * Note that it throws an Exception if the element is not found in the list
    * The exception message is "not found"*/
    public int indexOf(E data) throws Exception {
                                               
        SinglyNode<E> temp = head;     //current position
       
        int i = 0;                        //index
       
        for(i = 0; !(temp.data).equals(data) && temp != null; i++){    //try to find the data we are looking for
           
            if(i == size()) {
                throw new Exception("not found");            //check if not found
            }
           
            temp = temp.next;                       
        }
        return i;

    }

    /**Returns true if list is empty*/
    public boolean isEmpty() {
       
        if(head == null){
           
            return true;
           
        }else
           
            return false;
    }

    public void remove(E data) throws Exception {
         
         
            SinglyNode<E> current = head;
            SinglyNode<E> temp = null;   
         
            while(current != null ){
         
                if(current.data.equals(data)){
                    if(temp == null){
                        head = head.next;
                    }else{
                        temp.next = current.next;
                    }
                    size--;
                    return;
                }
                temp = current;
                current = current.next;
             
            }
        
        }

    public void set(int pos, E data) throws Exception {
       
        if(data == null){                                //check if item is null
           
            throw new NullPointerException();

        }else if(pos < 0 || pos > size) {               //check if the index is small than 1 or not
                                                        //check if the index is exist or not    
            throw new Exception("out of bounds");
           
        }else{
               
            SinglyNode<E> current = head;           //get a reference to the node object representing the index element
            for (int i = 0; i < pos; i++) {
                current = current.next;
            }
            current.data = data;
        }
    }

    /** Size of the list */
    public int size() {
       
        return size;
    }
    /** print list as string */
    public String toString() {

            SinglyNode<E> current = head;
            StringBuffer print = new StringBuffer();   //create the container for store the string
           
            if(isEmpty()){                            //print empty message if the list is empty
                System.out.print("<EMPTY>");
            }
            while (current != null) {                //loop through the list if the current node is not null
               
                print.append (current.data.toString()); //copy the data into string
                current = current.next;
             
              if(current != null) {                   
               
                print.append (" , ");
               
              }
             
            }
                return print.toString();
    }

}

Explanation / Answer

I have changed your code, Please check the code.

In for loop you are checking data and checking for null.

Here you have to check for null and check for data. that is correct only.

Please validate the code.


/**
     * Finds the index of the first occurrence of the specified element.
     * Note that it throws an Exception if the element is not found in the list
     * The exception message is "not found"*/
    public int indexOf(E data) throws Exception {
                                              
        SinglyNode<E> temp = head;     //current position
      
        int i = 0;                        //index
      
        //Here your are written wrongly, First you need to check temp != null and check data.
        for(i = 0; temp != null && !((temp.data).equals(data)) ; i++){    //try to find the data we are looking for
          
            if(i == size()) {
                throw new Exception("not found");            //check if not found
            }
          
            temp = temp.next;                      
        }
        return i;

    }