1

My main method:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String string1;
    string1 = input.next();

    LargeInteger firstInt = new LargeInteger(string1);

    System.out.printf("First integer: %s \n", firstInt.display());
}

LargeInteger class:

public class LargeInteger {

    private int[] intArray;

    //convert the strings to array
    public LargeInteger(String s) {
        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10); // in base 10
        }
    }

    //display the strings
    public String display() {
        String result = "";

        for (int i = 0; i < intArray.length; i++) {
            result += intArray[i];
        }
        return result.toString();
    }
}
4
  • Where is null pointer? What line? Commented Mar 22, 2012 at 17:21
  • It's generally helpful to include the error message and stack trace, since it will tell us exactly what line the problem happened on and why. Commented Mar 22, 2012 at 17:21
  • You should consult a debugger, but could it be because you never give intArray a size? Commented Mar 22, 2012 at 17:22
  • Yea sorry about that. It is definitely because I did not give a size. Commented Mar 22, 2012 at 17:29

6 Answers 6

7

You did not instantiate your array. You need something like:

   private int[] intArray = new int[SIZE];

where size is the length of your array.

Sign up to request clarification or add additional context in comments.

5 Comments

right dang.. Actually this is going to be for very large numbers who's length could be anything. So I think I am going to have to switch to an arrayList.
But in this case the size of the array is unknown
So isn't that the reason to use arrayList instead?
You do not need to use an arrayList. The length is known through the length of the string!
I was wrong! You do know the length of the array! it is s.length()
1

You are not initialize the array intArray, that way you are getting error, here is the complete program

import java.util.Scanner;

class  TestForNull {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String string1;
        string1 = input.next(); 

        LargeInteger firstInt = new LargeInteger(string1);

        System.out.printf ("First integer: %s \n", firstInt.display());
    }

}

and this is LargeInteger

public class LargeInteger {

    private int[] intArray;
    //convert the strings to array
    public LargeInteger(String s) {
        intArray = new int[s.length()];
        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10); // in base 10
        }
    }

    //display the strings
    public String display() {           
          String result="";

          for (int i = 0; i < intArray.length; i++) {     
            result += intArray[i];
          }
          return result.toString();
        }   
}

3 Comments

ah that makes sense! as a side question, the end goal of this assignment is to add large numbers without using the biginteger class. Is using an int array that can grow to be quite alot of numbers the right choice for the data structure?
generally speaking - my view don't believe in int if you are planning for large numbers, there are other data type too. So I can say that's not a right choice for the data structure. But this is dependent on developer and I test your program with "12345678901234567890123456789012345678901234567890" and its works for me.
yep works with some pretty arge numbers so I think I am pretty happy :)
1
private int[] intArray;

Member variables are null by default, so you need to initialize this.

Most likely you want it the same size as your string:

public LargeInteger(String s) {
    intArray = new int[s.length()]; // Create the actual array before you try to put anything in it
    for (int i = 0; i < s.length(); i++) {
        intArray[i] = Character.digit(s.charAt(i), 10); // in base 10
    }
}

Or you should use a container that resizes itself, like ArrayList.

Comments

1

Diferent approach through Integer.parseInt Integer.parseInt("yourInt");

To achieve your goal:

String a = "12345667788" //sample
String b = "";
int [] vecInt = new int[a.length()];  // The lack of initialization was your mistake as the above stated
for(int i=0; i< a.length(); i++)
{
   b = a.substring(0,1);
   a= a.substring(1);
   vecInt[i] = Integer.parseInt(b);
}

Please be aware of Double, long have far higher range then Integer which might be enough in your case to avoid an array!

Comments

0

you forgot to initialize the array intArray

I would recommend to use a java.util.List

Comments

0

You forgot to initialize the array. You have written it in constructor and the variables declared in method or constructor needs to be initialize at the same time.

Note : Implementing your logic in Constructor is not recommended unless and until you dont have any other choice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.