954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

my program is compiled.but on run that i have message "Exception in thread "main" jav

my program is compiled.but on run that i have message "Exception in thread "main" java.lang.NullPointer Exception"

1. class Rev
2. {
3. char a[];
4. char i=3;
5. void str()
6. {
7. for(i='A';i<'D';i++)
8. {
9. a[i]='i';
10. }
11. }
12. void show()
13. {
14. for(int i='A';i<'D';i++)
15. {
16. System.out.print(a[i]);
17. }
18. }
19. void reversStr()
20. {
21. for(i='A';i<'D';i++)
22. {
23. System.out.print(a[i]);
24. }
25. }
26. }
27. class Revers
28. {
29. public static void main(String[] args)
30. {
31. Rev a1=new Rev();
32. a1.str();
33. a1.show();
34. a1.reversStr();
35. }
36. }
37. plz run and solve the problem

ajay_tabbu
Junior Poster in Training
52 posts since Feb 2007
Reputation Points: 24
Solved Threads: 0
 

I think you should reconsider your for loop

for(i='A';i<'D';i++)


I never heard that in java you can increase value of character type variable, this is possible but in Perl
Create array of characters and with use of "i" get the character from the array
so if char[] myArray = { 'A', 'B', 'C'}
then if i = 1
you get character B

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Oh my.

For one, I think this is how you meant to represent your code:

class Rev {
    char a[];
    char i=3;
    
    void str() {
        for(i='A';i<'D';i++) {
            a[i]='i';
        }
    }
    
    void show() {
        for(int i='A';i<'D';i++) {
            System.out.print(a[i]);
        }
    }
    
    void reversStr() {
        for(i='A';i<'D';i++) {
            System.out.print(a[i]);
        }
    }
    
}

class Revers {
    
    public static void main(String[] args) {
        Rev a1=new Rev();
        a1.str();
        a1.show();
        a1.reversStr();
    }
    
}


And, I get the error because you're trying to access an array element without having defined the size of the array.

void str() {
    for(i='A';i<'D';i++) {
        a[i]='i'; // ERROR OCCURS HERE
    }
}


So, define a length for the array at the start of class Rev: char[] a = new char[(some integer)];

You're still going to end up with issues, such asjava.lang.ArrayIndexOutOfBoundsException: 65 when you try to access a['A'] and so on, because 'A' as a char is 65 as an integer, and integers are what you want to work with when accessing arrays.

I have no idea what your program's trying to do, but those are some thing to consider.

Cudmore
Junior Poster in Training
74 posts since Nov 2005
Reputation Points: 20
Solved Threads: 6
 

thank you very much

ajay_tabbu
Junior Poster in Training
52 posts since Feb 2007
Reputation Points: 24
Solved Threads: 0
 

i m very thankfull 4 r u r help

ajay_tabbu
Junior Poster in Training
52 posts since Feb 2007
Reputation Points: 24
Solved Threads: 0
 
class addition
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a+b;
System.out.println("sum="+c);
}
}
2bikash
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

Your code only works if args.length >= 2 and if the args can be parsed as integers. In short, it is almost useless. As is upping a thread that is 3 years old.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You