Java 我的数组得到空指针异常?

Java 我的数组得到空指针异常?,java,arrays,string,nullpointerexception,Java,Arrays,String,Nullpointerexception,当我尝试将字符串转换为int数组时,我得到一个空指针异常。我的字符串数组未被使用 如何将整型数组更改为字符串数组,或者如何解决问题 这是我的密码 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication3; /** * * @author Ivan Beazer */ import java.io.

当我尝试将字符串转换为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;
   }
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包javaapplication3;
/**
*
*@作者伊万·比泽
*/
导入java.io.*;
/**
这个程序演示了中的搜索方法
IntBinarySearcher类。
*/
公共类二进制搜索测试
{
私有静态字符串限制;
//将字符串数组转换为字符串
公共静态字符串arrayToString2(字符串[]个字,字符串aString)
{
StringBuilder结果=新建StringBuilder();
如果(words.length>0)
{
结果.追加(字[0]);

对于(int i=1;i你得到的是一个NullPointerException,因为变量“aString”在你说“问题出在这里”的那一行是空的。你从来没有将它设置为任何值。我不能说我真的理解你在这里试图做什么,所以我无能为力。

你得到一个NullPointerException,因为变量“aString”在你说“问题出在这里”的那一行是空的。你从来没有设置过它。我不能说我真的理解你在这里要做什么,所以我不能再帮你了。

你不需要if(numbers!=null),因为“numbers”永远不能为空(除非发生OOME,这里不是这样),因为您在前几行对其进行了实例化。但问题可能是因为您的搜索可能为空。

您不需要if(number!=null),因为“number”永远不能为空(除非发生OOME,这里不是这种情况),因为您在几行之前实例化了它。但问题可能是因为您的aString可能为空。

aString在哪里初始化?我想这就是为什么您会得到一个空指针执行选项。

aString在哪里初始化?我想这就是为什么您会得到一个空指针执行选项。

变量aStr在哪里ing已初始化?变量aString在哪里初始化?我不明白我使用字符串生成器将aString设置为Word数组。在程序中,您从未说过“aString=X”,表示X的任何值。每次您提到aString(包括代码中的StringBuilder部分)时,您都在读取aString的值,而不是将其设置为任何值。好的,现在我看到了我在做什么。我不明白我使用StringBuilder将aString设置为Word数组。在程序中,您从未说过“aString=X”,表示任何X值。每次您提到aString(包括代码中的StringBuilder部分)时,您都在读取aString的值,而不是将其设置为任何值。好的,现在我看到我在做什么了。