Skip to main content
 首页 » 编程设计

java之我的数组出现空指针异常

2024年04月12日122over140

当我尝试将字符串转换为 int 数组时,出现空指针异常。我的字符串数组未被使用。

如何将 int 数组更改为字符串数组或如何解决我的问题?

这是我的代码。

/* 
 * To change this template, choose Tools | Templates 
 * and open the template in the editor. 
 */ 
 
package javaapplication3; 
 
/** 
 * 
 * @author Ivan Beazer 
 */ 
import java.io.*; 
 
/** 
   This program demonstrates the search method in 
   the IntBinarySearcher class. 
*/ 
 
public class BinarySearchTest  
{ 
    private static String aString; 
    // Convert string array to string 
    public static String arrayToString2(String[] words, String aString)  
    { 
        StringBuilder result = new StringBuilder(); 
        if (words.length > 0)  
        { 
            result.append(words[0]); 
            for (int i=1; i<words.length; i++)  
            { 
                result.append(aString); 
                result.append(words[i]); 
            } 
        } 
        return result.toString(); 
    }     
 
   public static void main(String [] args) throws IOException 
   { 
      int result, searchValue; 
      String input; 
 
      // A String array of words to search. 
      // This is the error. netbeans says it's not being used. 
      String[] words = {"Jake", "Jerry", "Bill", "Lousie", "Goku", "Ivan", "John", "sarah", "kim"}; 
 
 
 
      // convert string to int array 
 
      // this is my problem 
      int[] numbers = new int[aString.length()]; 
      for(int i=0; i<aString.length(); i++) 
        numbers[i] = Character.getNumericValue(aString.charAt(i));   
 
      // this is me trying to fix the problem 
      if(numbers != null) 
        { 
            //do something to num 
 
        } 
 
      // Create the console input objects. 
      InputStreamReader reader = 
                 new InputStreamReader(System.in); 
      BufferedReader keyboard = 
                 new BufferedReader(reader); 
 
      // First we must sort the array in ascending order. 
      IntQuickSorter.quickSort(numbers); 
 
      do 
      { 
         // Get a value to search for. 
         System.out.print("Enter a value to search for: "); 
         input = keyboard.readLine(); 
         searchValue = Integer.parseInt(input); 
 
         // Search for the value 
         result = IntBinarySearcher.search(numbers, searchValue); 
 
        // Display the results. 
        if (result == -1) 
           System.out.println(searchValue + " was not found."); 
        else 
        { 
           System.out.println(searchValue + " was found at " + 
                              "element " + result); 
        } 
 
        // Does the user want to search again? 
        System.out.print("Do you want to search again? (Y or N): "); 
        input = keyboard.readLine(); 
      } while (input.charAt(0) == 'y' || input.charAt(0) == 'Y'); 
   } 
} 
 
/* 
 * To change this template, choose Tools | Templates 
 * and open the template in the editor. 
 */ 
 
package javaapplication3; 
 
/** 
 * 
 * @author Devon B 
 */ 
/** 
   The IntBinarySearcher class provides a public static 
   method for performing a binary search on an int array. 
*/ 
 
public class IntBinarySearcher 
{ 
 
 
 
   /** 
      The search method performs a binary search on an int 
      array. The array is searched for the number passed to 
      value. If the number is found, its array subscript is 
      returned. Otherwise, -1 is returned indicating the 
      value was not found in the array. 
      @param array The array to search. 
      @param value The value to search for. 
   */ 
 
   public static int search(int[] array, int value) 
   { 
      int first;       // First array element 
      int last;        // Last array element 
      int middle;      // Mid point of search 
      int position;    // Position of search value 
      boolean found;   // Flag 
 
      // Set the inital values. 
      first = 0; 
      last = array.length - 1; 
      position = -1; 
      found = false; 
 
      // Search for the value. 
      while (!found && first <= last) 
      { 
         // Calculate mid point 
         middle = (first + last) / 2; 
 
         // If value is found at midpoint... 
         if (array[middle] == value) 
         { 
            found = true; 
            position = middle; 
         } 
         // else if value is in lower half... 
         else if (array[middle] > value) 
            last = middle - 1; 
         // else if value is in upper half.... 
         else 
            first = middle + 1; 
      } 
 
      // Return the position of the item, or -1 
      // if it was not found. 
      return position; 
 
 
   } 
} 
 
/* 
 * To change this template, choose Tools | Templates 
 * and open the template in the editor. 
 */ 
 
package javaapplication3; 
 
/** 
 * 
 * @author Devon B 
 */ 
/** 
   The IntQuickSorter class provides a public static 
   method for performing a QuickSort on an int array. 
*/ 
 
public class IntQuickSorter 
{ 
  /** 
      The quickSort method calls the doQuickSort method 
      to sort an int array. 
      @param array The array to sort. 
   */ 
 
   public static void quickSort(int array[]) 
   { 
      doQuickSort(array, 0, array.length - 1); 
   } 
 
   /** 
      The doQuickSort method uses the QuickSort algorithm 
      to sort an int array. 
      @param array The array to sort. 
      @param start The starting subscript of the list to sort 
      @param end The ending subscript of the list to sort 
   */ 
 
   private static void doQuickSort(int array[], int start, int end) 
   { 
      int pivotPoint; 
 
      if (start < end) 
      { 
         // Get the pivot point. 
         pivotPoint = partition(array, start, end); 
 
         // Sort the first sub list. 
         doQuickSort(array, start, pivotPoint - 1); 
 
         // Sort the second sub list. 
         doQuickSort(array, pivotPoint + 1, end); 
      } 
   } 
 
   /** 
      The partiton method selects a pivot value in an array 
      and arranges the array into two sub lists. All the 
      values less than the pivot will be stored in the left 
      sub list and all the values greater than or equal to 
      the pivot will be stored in the right sub list. 
      @param array The array to partition. 
      @param start The starting subscript of the area to partition. 
      @param end The ending subscript of the area to partition. 
      @return The subscript of the pivot value. 
   */ 
 
   private static int partition(int array[], int start, int end) 
   { 
      int pivotValue;    // To hold the pivot value 
      int endOfLeftList; // Last element in the left sub list. 
      int mid;           // To hold the mid-point subscript 
 
      // Find the subscript of the middle element. 
      // This will be our pivot value. 
      mid = (start + end) / 2; 
 
      // Swap the middle element with the first element. 
      // This moves the pivot value to the start of 
      // the list. 
      swap(array, start, mid); 
 
      // Save the pivot value for comparisons. 
      pivotValue = array[start]; 
 
      // For now, the end of the left sub list is 
      // the first element. 
      endOfLeftList = start; 
 
      // Scan the entire list and move any values that 
      // are less than the pivot value to the left 
      // sub list. 
      for (int scan = start + 1; scan <= end; scan++) 
      { 
         if (array[scan] < pivotValue) 
         { 
            endOfLeftList++; 
            swap(array, endOfLeftList, scan); 
         } 
      } 
 
      // Move the pivot value to end of the 
      // left sub list. 
      swap(array, start, endOfLeftList); 
 
      // Return the subscript of the pivot value. 
      return endOfLeftList; 
   } 
 
   /** 
      The swap method swaps the contents of two elements 
      in an int array. 
      @param The array containing the two elements. 
      @param a The subscript of the first element. 
      @param b The subscript of the second element. 
   */ 
 
   private static void swap(int[] array, int a, int b) 
   { 
      int temp; 
 
      temp = array[a]; 
      array[a] = array[b]; 
      array[b] = temp; 
   } 
} 

请您参考如下方法:

您将收到 NullPointerException,因为变量“aString”在您说“这是问题所在”的那一行为空。你从来没有将它设置为任何东西。我不能说我真的理解你在这里想做什么,所以我也无能为力。