我的阵列得到一个空指针异常? [英] My array gets a Null Pointer Exception?

查看:163
本文介绍了我的阵列得到一个空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将字符串转换为int数组,我得到一个空指针异常。不被使用我的字符串数组。

如何改变一个int数组转换成字符串数组或如何解决我的问题?

下面是我的code。

  / *
 *要改变这种模板,选择Tools |模板
 *并打开编辑器的模板。
 * /包javaapplication3;/ **
 *
 * @author伊万Beazer上市
 * /
进口java.io. *;/ **
   此程序演示了搜索方法
   在IntBinarySearcher类。
* /公共类BinarySearchTest
{
    私人静态字符串ASTRING;
    //转换字符串数组串
    公共静态字符串arrayToString2(字符串[]的话,字符串ASTRING)
    {
        StringBuilder的结果=新的StringBuilder();
        如果(words.length大于0)
        {
            result.append(字[0]);
            的for(int i = 1; I< words.length;我++)
            {
                result.append(ASTRING);
                result.append(字由[i]);
            }
        }
        返回result.toString();
    }   公共静态无效的主要(字串[] args)抛出IOException异常
   {
      INT结果,searchValue;
      字符串输入;      //词的字符串数组进行搜索。
      //这是错误。 NetBeans的说,它不被使用。
      的String []字= {杰克,杰里,条例,Lousie,悟空,伊万,约翰,莎拉,金};      //字符串转换为int数组      //这是我的问题
      INT []号=新INT [aString.length()];
      的for(int i = 0; I< aString.length();我++)
        号码[I] = Character.getNumericValue(aString.charAt(ⅰ));      //这是我试图解决这个问题
      如果(数字!= NULL)
        {
            //做一些NUM        }      //创建控制台输入对象。
      InputStreamReader的读卡器=
                 新的InputStreamReader(System.in);
      BufferedReader中的键盘=
                 新的BufferedReader(读者);      //首先,我们必须按升序排序的数组。
      IntQuickSorter.quickSort(数字);      做
      {
         //获取一个值来搜索。
         System.out.print(输入要搜索的值:);
         输入= keyboard.readLine();
         sea​​rchValue =的Integer.parseInt(输入);         //搜索的值
         结果= IntBinarySearcher.search(数字,searchValue);        //显示结果。
        如果(结果== -1)
           的System.out.println(searchValue +没有被发现。);
        其他
        {
           的System.out.println(searchValue +在发现+
                              元素+结果);
        }        //用户是否要再次搜索?
        System.out.print(你想再次搜索(是或否)?);
        输入= keyboard.readLine();
      }而(input.charAt(0)=='Y'|| input.charAt(0)=='Y');
   }
}/ *
 *要改变这种模板,选择Tools |模板
 *并打开编辑器的模板。
 * /包javaapplication3;/ **
 *
 * @author德文乙
 * /
/ **
   该IntBinarySearcher类提供了一个公共静态
   方法用于执行int数组的二进制搜索。
* /公共类IntBinarySearcher
{   / **
      搜索方法执行上一个int二进制搜索
      数组。该阵列中搜索传递给数
      值。如果发现的数量,其数组下标是
      回。否则,返回-1指示
      值没有在数组中找到。
      @参数数组数组进行搜索。
      @参数值来搜索的值。
   * /   公共静态INT搜索(INT []数组,int值)
   {
      诠释第一; //一个数组元素
      INT最后; //最后一个数组元素
      INT中间;搜索//中间点
      INT位置; //搜索值的位置
      布尔发现; //下垂      //设置inital值。
      第一= 0;
      最后= array.length - 1;
      位置= -1;
      找到= FALSE;      //搜索的价值。
      而(发现和放大器;!&放大器;第一< =上)
      {
         //计算中点
         中间=(第一+最后一个)/ 2;         //如果值在中点发现...
         如果(阵列[中] ==值)
         {
            发现= TRUE;
            位置=中间;
         }
         //否则,如果值是下半...
         否则,如果(阵列[中]≥值)
            最后=中间 - 1;
         //否则,如果值是上半....
         其他
            第一=中间+ 1;
      }      //返回项的位置,或-1
      //如果没有被发现。
      返回的位置;
   }
}/ *
 *要改变这种模板,选择Tools |模板
 *并打开编辑器的模板。
 * /包javaapplication3;/ **
 *
 * @author德文乙
 * /
/ **
   该IntQuickSorter类提供了一个公共静态
   方法int数组上执行快速排序。
* /公共类IntQuickSorter
{
  / **
      快速排序方法调用doQuickSort方法
      排序为int数组。
      @参数数组数组进行排序。
   * /   公共静态无效的快速排序(int数组[])
   {
      doQuickSort(数组,0,array.length - 1);
   }   / **
      该doQuickSort方法使用快速排序算法
      排序为int数组。
      @参数数组数组进行排序。
      @Param启动列表的起始标排序
      @param结束列表的结束标排序
   * /   私有静态无效doQuickSort(int数组[],诠释开始,诠释完)
   {
      INT pivotPoint;      如果(开始与LT端)
      {
         //获取支点。
         pivotPoint =分区(阵,开始,结束);         //排序的第一个子列表。
         doQuickSort(数组,启动,pivotPoint - 1);         //排序第二子列表。
         doQuickSort(数组,pivotPoint + 1结束);
      }
   }   / **
      该partiton方法在数组中选择枢轴值
      和安排所述阵列分成两个子列表。一切
      值小于所述枢轴将存储在左
      子清单和所有的值大于或等于
      枢轴将存储在右子列表。
      @参数数组的数组进行分区。
      @Param开始进行分区的区域的起始标。
      @参数结束分区的区域的结束标。
      返回:枢轴值的下标。
   * /   私有静态诠释分区(int数组[],诠释开始,诠释完)
   {
      INT pivotValue; //要保持价值支点
      INT endOfLeftList;在左子清单//最后一个元素。
      INT中旬; //要保持中点标      //查找中间元素的下标。
      //这将是我们的支点价值。
      中期=(START +前端)/ 2;      //与第一元件交换的中间元件。
      //这个支点值移动到开始
      // 列表。
      掉期(数组,启动,中旬);      //保存比较枢轴值。
      pivotValue =阵列[开始]。      //现在,左子列表的末尾是
      //第一个元素。
      endOfLeftList =启动;      //扫描整个列表和移动任何价值了
      //小于枢轴值向左
      //子列表。
      对于(INT扫描=启动+ 1;扫描< =结束;扫描++)
      {
         如果(阵列[扫描]< pivotValue)
         {
            endOfLeftList ++;
            掉期(数组,endOfLeftList,扫描);
         }
      }      //移动支点值结束的
      //左子列表。
      掉期(数组,启动endOfLeftList);      //返回枢轴值的下标。
      返回endOfLeftList;
   }   / **
      交换方法交换两个元素的含量
      在一个int数组。
      @Param含有两个元素的数组。
      @Param第一元件的一个下标。
      @Param b中的第二元素的下标。
   * /   私有静态无效掉期(INT []数组,int类型的,INT B)
   {
      INT温度;      TEMP =阵列[A];
      数组[A] =阵列[B]。
      数组[B] =温度;
   }
}


解决方案

您会得到一个NullPointerException异常,因为变量ASTRING是在您说行空这里有一个问题。你从来没有将其设置为任何事情。我不能说我真的明白你试图在这里做什么,所以我忍不住比了。

When I try to convert a string to int array, i get a Null Pointer Exception. My String array isn't being used.

How do you change a int array into a string array or how do I fix my problem?.

Here is my code.

/*
 * 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;
   }
}

解决方案

You're getting a NullPointerException because the variable "aString" is null at the line where you say "here's the problem." You've never set it to anything. I can't say I really understand what you're trying to do here, so I can't help any more than that.

这篇关于我的阵列得到一个空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆