-2
 import java.io.*;
 import java.util.Scanner;
 import java.math.*;

 class Sort {

 public static void main(String args[]) {

    class gint {
        int N, n, s;
        int ggn[];
    }

    gint gginta=new gint();
    gint ggintb=new gint();
    gint ggintc=new gint();

    int i=0;
    int j=0;
    char c[];
    String ss;
    String sggn;
    String sggnrev="";


    System.out.println("enter the number of maximum digits of the number");
    Scanner scan=new Scanner(System.in);
    gginta.N=scan.nextInt();
    System.out.println("enter the number exact digits of the number ");
    gginta.n=scan.nextInt();
    System.out.println("enter the sign of the number");

    System.out.println("enter the number");
    sggn=scan.next();

    char temp[];
    int l=sggn.length();

    for(j=l-1; j >= 0; j--) {
        sggnrev= sggnrev+sggn.charAt(j);
    }
    int x= Integer.parseInt(sggn);
    System.out.println(sggnrev);
    System.out.println(sggn.length());
    int h;
    System.out.println(sggn.length());

    for (int f=0;f<=sggn.length()-1;f++) {
        System.out.println(f);
        double y=(int) (x/Math.pow(10, f));
        double z=(int) (x/Math.pow(10, f+1));
        double w= y-(z*10);
        System.out.println("power"+i+"= "+(int) y +"-"+(int)z*10+"="+w);
        h= (int)  w;

        gginta.ggn[f]=h;

        System.out.print(gginta.ggn[l-1]);
        l--;
    }
    for (i=0;i<=sggn.length()-1;i++) {          
        if (sggn.length()!=gginta.n) {
            System.out.println("number not in range....enter the number again ... ");
            do {
                System.out.println("number not in range....enter the number again ... ");           
                System.out.println("enter the number");
                sggn=scan.next();       
            } while (sggn.length()!=gginta.n);
        }
    }
}

this code is basically taking a number as a string and i am aiming to get each index of the string and transfer its value to the index of an integer array so that i have my number is in the integer array and each index of the array represents a digit of the number

elias
  • 15,010
  • 4
  • 40
  • 65
Mudit
  • 69
  • 1
  • 11
  • 7
    What's your question? You're just dumping your code here. – Sotirios Delimanolis Feb 19 '14 at 15:57
  • 3
    add the stacktrace of your exception – amudhan3093 Feb 19 '14 at 15:59
  • 2
    The Null Pointer Exception will tell you which line of the code is failing, and how you got there, if you look at the stack trace. Find that line, figure out what pointers are being used on that line, figure out how any of them could possibly be null, fix the code. – keshlam Feb 19 '14 at 15:59
  • Also, please read up on how to format code in SO. It's very hard to understand what you posted. – Taylor Feb 19 '14 at 16:01
  • [A good way to debug nullPointerException](http://stackoverflow.com/questions/17871825/a-good-way-to-debug-nullpointerexception) – pamphlet Feb 19 '14 at 16:02
  • 1
    `class gint { int N, n, s; int ggn[]; }` really...? Good luck understanding code like this five days from today. If you really think the naming conventions of the Java language are not for you, at least be consistent within your own style. Everything else just leads to chaos and confusion. ;) – sheltem Feb 19 '14 at 16:03
  • What a mess! But I bet it's because the `ggn` array was not initialized. – elias Feb 19 '14 at 16:04
  • You try to use the method parseInt in someting that isn't an int in line `52` (it has the signal of the number with the number). And in line `66` you try to access a position of a non-initialized vector. That gives to you another NPE. – ddz Feb 19 '14 at 16:05
  • @user3077245: Elias is right. `ggn` is not initialized anywhere. – Ravinder Reddy Feb 19 '14 at 16:09
  • @SotiriosDelimanolis - Its a part of my homework...what i have to do is that i have to input a number that has any number of digits and i have to store it in an integer/double array so that each index of the array has two digits of the number. – Mudit Feb 19 '14 at 16:17
  • Part of your homework is to dump your code on stackoverflow? – Sotirios Delimanolis Feb 19 '14 at 16:18
  • @SotiriosDelimanolis - i am just asking for some help.... u dont want to ... sit back and relax – Mudit Feb 19 '14 at 16:24
  • @Elias: i dont know , how to initialize an array without maximum bound – Mudit Feb 19 '14 at 16:25
  • You can ask a question, but follow the guidelines in the [help center](http://stackoverflow.com/help). In this case, you are dumping all your code and expecting us to debug it for you. No, that's your work. Your question shows complete lack of effort. – Sotirios Delimanolis Feb 19 '14 at 16:26
  • @SotiriosDelimanolis : thank you for guiding me...i appreciate but FYI i was looking for a suggestion on where am i going wrong and any alternatives to solve the problem such as to use Arraylist instead of an Array .... – Mudit Feb 19 '14 at 16:42
  • Changing data structures won't affect anything. A `NullPointerException` occurs when you try to dereference a `null` reference. Some variable or expression of yours is being resolved to `null`, possibly because uou haven't initialized something somewhere. That's what you need to debug. – Sotirios Delimanolis Feb 19 '14 at 16:44
  • when i am initializing the array everything is working fine.... but my professor wants this array to have an infinite bound – Mudit Feb 19 '14 at 16:50
  • @user3077245 post what the input values you're using and the stacktrace, otherwise it'll be dificult to help you. – elias Feb 19 '14 at 17:03

2 Answers2

0

Just initialize the ggn array in gint class:

int ggn[] = new int[5];

Otherwise the program is dereferencing a field with a null value

gginta.ggn[f]=h;
Boris
  • 22,667
  • 16
  • 50
  • 71
-1

Always use int x= Integer.parseInt(sggn); in try-catch block

int x =0;

try {
x= Integer.parseInt(sggn);
}catch(NumberFormatException ex) {
 //handle exception:
 x =0;
}
Archie Yalakki
  • 512
  • 4
  • 12