Java Why am I getting a NullPointerException while instantiating my array -


i new programming , don't why program gives me run time error nullpointerexception when have tried initializing n, numint, , arraymenu. none of seem work. program's job gather set of random integers store in array , allow user pick sort choose from. reading.

import java.util.scanner; import java.util.random; public class varioussortshs {     private static int[] arraymenu;     private static random generator;      /**      * constructor objects of class varioussortshs.      */     public varioussortshs(int n) //the error starts here     {                                         arraymenu = new int[n];  //i don't why says null in array when                                  //i initializing length of array n         /*assigns random number between 0 100.*/         for(int = 0; < n; i++)          {             int temp = generator.nextint(100);             arraymenu[n] = temp;         }            }      /**      * selection sort method.      */     public static void selection(int n)     {         for(int = 0; < arraymenu.length - 1; i++)         {             int minpos = i;             for(int j = + 1; j < arraymenu.length; j++)             {                 if(arraymenu[j] < arraymenu[minpos]) minpos = j;             }             int temp = arraymenu[i];             arraymenu[i] = arraymenu[minpos];             arraymenu[minpos] = temp;             system.out.print(temp + " ");         }     }      /**      * insertion sort method.      */     public static void insertion(int n)     {         for(int = 1; < arraymenu.length; i++)         {             int next = arraymenu[i];             int j = i;             while(j > 0 && arraymenu[j - 1] > next)             {                 arraymenu[j] = arraymenu[j - 1];                 j--;             }             arraymenu[j] = next;             system.out.print(next + " ");         }     }      /**      * quick sort method.      */     public static void quick(int n)     {         int pivot = arraymenu[0];         int = 0 - 1;         int j = n + 1;         while(i < j)         {             i++; while(arraymenu[i] < pivot) i++;             j++; while(arraymenu[j] > pivot) j++;             if(i < j)             {                 int temp = arraymenu[i];                 arraymenu[i] = arraymenu[j];                 arraymenu[j] = temp;                 system.out.print(temp + " ");             }         }     }      /**      * main method allows user input data.       */     public static void main(string[] args)     {          scanner in = new scanner(system.in);         system.out.println("do wish sort random integers? (yes or no) ");         string answer = in.next();         string answer2 = answer.tolowercase();                  {             /*prompts array length.*/             system.out.println("how many random integers wish sort?");             int numint = in.nextint();             /*promps sort selection choice.*/             system.out.println("select sort use: \n\t1)selection\n\t2)insertion\n\t3)quick");             string sort = in.next();             string sort2 = sort.tolowercase();             if(sort2.equals("selection"))             {                 selection(numint);             }             else if(sort2.equals("insertion"))             {                 insertion(numint);             }             else if(sort2.equals("quick"))             {                 quick(numint);             }             else             {                 system.out.println("you have entered wrong input.");             }         } while(!answer2.equals("no"));     } } 

  • everything in code static. means constructor wrote never called, , array has never been changed default value, null. consider changing constructor code static initialization block instead.
  • generator never set anything, it's null , can't call nextint on it
  • initializing array setting arraymenu[n] instead of arraymenu[i]

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -