2011-03-16 7 views
1

문자열을 int 배열로 변환하려고하면 Null 포인터 예외가 발생합니다. 문자열 배열이 사용되지 않습니다.내 배열에서 Null 포인터 예외가 발생합니까?

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; 
    } 
} 
+0

변수 * aString *은 어디에 초기화됩니까? – Raghuram

답변

4

"여기에 문제가 있습니다"라는 줄에서 "aString"변수가 null이기 때문에 NullPointerException이 발생합니다. 당신은 결코 그것을 아무것도 설정하지 않았습니다. 네가 여기서 뭘 하려는지 정말 이해한다고 말할 수 없어, 그 이상으로 도울 수는 없어.

+0

aString을 words 배열로 설정하는 문자열 작성기를 사용하는 것을 이해하지 못합니다. – lonesarah

+0

프로그램에서 아무 것도 X의 값에 대해 "aString = X"라고 말하지 않습니다. 코드의 StringBuilder 부분을 포함하여 aString을 말할 때마다 * aString 값을 읽거나 설정하지 않습니다. 그것은 무엇이든합니다. –

+0

좋아, 지금 내가 뭘하는지보고있어. – lonesarah

0

"numbers"는 null이 될 수 없으므로 필요하지는 않습니다 (OOME이 발생하지 않는 한 여기에 해당되지 않습니다). 이전에 몇 줄을 인스턴스화했기 때문에 null이 될 수 없습니다. 하지만 문제는 아마도 aString이 null 일 수 있기 때문일 수 있습니다.

0

aString을 초기화하는 위치는 어디입니까? 나는 당신이 null pointer execption을 얻는 이유를 말할 것이다.

관련 문제