943,559 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2617
  • Java RSS
Feb 7th, 2007
0

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

Expand Post »
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
Similar Threads
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
ajay_tabbu is offline Offline
52 posts
since Feb 2007
Feb 7th, 2007
1

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

I think you should reconsider your for loop

Java Syntax (Toggle Plain Text)
  1. 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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Feb 7th, 2007
1

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

Oh my.

For one, I think this is how you meant to represent your code:
java Syntax (Toggle Plain Text)
  1. class Rev {
  2. char a[];
  3. char i=3;
  4.  
  5. void str() {
  6. for(i='A';i<'D';i++) {
  7. a[i]='i';
  8. }
  9. }
  10.  
  11. void show() {
  12. for(int i='A';i<'D';i++) {
  13. System.out.print(a[i]);
  14. }
  15. }
  16.  
  17. void reversStr() {
  18. for(i='A';i<'D';i++) {
  19. System.out.print(a[i]);
  20. }
  21. }
  22.  
  23. }
  24.  
  25. class Revers {
  26.  
  27. public static void main(String[] args) {
  28. Rev a1=new Rev();
  29. a1.str();
  30. a1.show();
  31. a1.reversStr();
  32. }
  33.  
  34. }

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

Java Syntax (Toggle Plain Text)
  1. void str() {
  2. for(i='A';i<'D';i++) {
  3. a[i]='i'; // ERROR OCCURS HERE
  4. }
  5. }

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 as java.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.
Reputation Points: 20
Solved Threads: 6
Junior Poster in Training
Cudmore is offline Offline
74 posts
since Nov 2005
Feb 7th, 2007
0

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

thank you very much
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
ajay_tabbu is offline Offline
52 posts
since Feb 2007
Feb 7th, 2007
0

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

i m very thankfull 4 r u r help
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
ajay_tabbu is offline Offline
52 posts
since Feb 2007
Mar 1st, 2010
0

add program

Java Syntax (Toggle Plain Text)
  1. class addition
  2. {
  3. public static void main(String args[])
  4. {
  5. int a=Integer.parseInt(args[0]);
  6. int b=Integer.parseInt(args[1]);
  7. int c=a+b;
  8. System.out.println("sum="+c);
  9. }
  10. }
Last edited by peter_budo; Mar 1st, 2010 at 6:34 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
2bikash is offline Offline
1 posts
since Mar 2010
Mar 1st, 2010
0
Re: my program is compiled.but on run that i have message "Exception in thread "main" jav
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Program desining issue
Next Thread in Java Forum Timeline: Menu and Input Confirmation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC