Domanda

I want to create a class and use it as a data type to create a MergeSort program using array. I created to program but it's giving me null pointer exception and I don't know why.

Employee Class

public class Employee implements Comparable<Employee>
{
private String firstname;
private int idNumber;
private String lastname;


  Employee(String fname, String lname, int id)
{  
  firstname = fname;
  idNumber = id;
  lastname = lname;

}


public void setFirstName(String fname)
    {firstname = fname;}
public String getFirstName()
    {return firstname;}

public void setidNumber(int id)
     {idNumber = id;}
public int getidNumber()
    {return idNumber;}

public void setLastName(String lname)
    {lastname = lname;}
public String getLastName() 
    {return lastname;}


public String toString()
{
    String str =  "\nName: " + firstname + " " + lastname
                + "\nID: "  + idNumber;

    return str;
}
public int compareTo(Employee Employee2)
    {
    int nameDiff = lastname.compareToIgnoreCase(Employee2.lastname);
    if(nameDiff != 0)

    return nameDiff;
    return -1;

       }
    }

Array Class

  class Array
    {
public Employee[] A;
public int nElems;

public Array(int max)
{
    A = new Employee[max];
    nElems = 0;
}
    public void insert(Employee value)
      {
         int j;
         for(j = 0; j < nElems; j++)
         if(A[j].equals(value))
         break;
         for(int k = nElems; k > j; k--)
         A[k] = A[k-1];
         A[j] = value;
         nElems++;
       }
public void display()
    {
    for(int j = 0; j < nElems; j++)
     System.out.print(A[j] + " ");
    System.out.println(" ");
     }

    public static void merge(Employee[] L, Employee[] R, Employee[] A)
{
    int nL = L.length;
    int nR = R.length;
    int i = 0;
    int j = 0;
    int k = 0;

    while(i < nL && j < nR)
    {
        if(L[i].compareTo(R[j]) <= 0)
        {
            A[k] = L[i];
            k++;
            i++;
        }
        else
        {
            A[k] = R[j];
            k++;
            j++;
        }
    }
    while(i < nL)
    {
        A[k] = L[i];
        k++;
        i++;
    }
    while (j < nR)
    {
        A[k] = R[j];
        k++;
        j++;
    }
}
private static void _mergeSort(Employee[] A)
{
    int n = A.length;
    int i , j, mid;

    if(n < 2) return;

    mid = n/2;
    Employee[] left = new Employee[mid];
    Employee[] right = new Employee[n - mid];

    for(i = 0; i < mid; i++)
        left[i] = A[i];
    for(i = mid; i < n-mid; i++)
        right[i-mid] = A[i];
    _mergeSort(left);
    _mergeSort(right);
    merge(left, right, A);

}
public void mergeSort()
{
    _mergeSort(A);
}            
}

Driver Class

class MergeSortDriver
{
public static void main (String [] args)
{
  Employee s1 = new Employee("Bob","Sullivan",52);      
  Employee s2 = new Employee("Maggie", "Smith",98);
  Employee s3 = new Employee("John", "Ocasio", 85);
  Employee s4 = new Employee("Christina", "Yang", 46);

  Array arr = new Array(50);

  Employee A[] = {s1,s2,s3,s4};

  arr.insert(s1);
  arr.insert(s2);
  arr.insert(s3);
  arr.insert(s4);

  arr.display();

  arr.mergeSort();   

  arr.display();   
     }
  }      

Error Mesg

   ----jGRASP exec: java MergeSortDriver
    Name: Bob Sullivan
    ID: 52 
    Name: Maggie Smith
    ID: 98 
    Name: John Ocasio
    ID: 85 
    Name: Christina Yang
    ID: 46  
    Exception in thread "main" java.lang.NullPointerException
at Employee.compareTo(Employee.java:37)
at Array.merge(Array.java:40)
at Array._mergeSort(Array.java:83)
at Array._mergeSort(Array.java:82)
at Array._mergeSort(Array.java:81)
at Array._mergeSort(Array.java:81)
at Array._mergeSort(Array.java:81)
at Array._mergeSort(Array.java:81)
at Array.mergeSort(Array.java:88)
at MergeSortDriver.main(Array.java:112)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
È stato utile?

Soluzione

First of all, either only make an array of size 4 (or whatever size you are actually going to use) or split the array into arrays that have valid values (not null).

Then, in your second for loop where you split the array into the left and right side, your condition for the loop says that i < n - mid but since you're starting at i = mid you don't make the array large enough.

Change it to this and you will no longer get the exception:

for(i = mid; i < n; i++)
    right[i-mid] = A[i];

Altri suggerimenti

Your NullPointerException is happening in the Employee#compareTo function -- make sure to read your stack trace carefully.

It's probably this line:

    int nameDiff = lastname.compareToIgnoreCase(Employee2.lastname);

If Employee2 is null, then this will fail because null doesn't have a field called lastname.

OK so what calls that? In your merge method, you do this:

if(L[i].compareTo(R[j]) <= 0)

Are you absolutely, 100% positive that R[j] != null? Your code doesn't check. Check that. Because if R[j] == null then this NullPointerException totally makes sense, right?

Also, no offense but this looks like Computer Science homework to me. Make sure to post next time what you've already tried, and what your thought process is. This can be largely taken care of by reading the stack trace really carefully.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top