• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to print from 1 to 9 in a label ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i m trying to print 1 to 10 in a jlabel but the code end up with just 9 in the frame and it skipped all the integers from 1 to 8
now how to print from 1 to 9 in a label ?

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are having only one label. So it shows the last set value using the setText() method in the for loop. You could either create 10 labels or add a button and an action listener to modify the text of the label to increment every time the button is clicked.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:You are having only one label. So it shows the last set value using the setText() method in the for loop. You could either create 10 labels or add a button and an action listener to modify the text of the label to increment every time the button is clicked.



yes but if i go for 10 label and single for loop to print values all the 10 labels will end up with 9 9 9 the end values so alternate way is to create 10 labels and 10 for loops each with different end values so that label 1 will set 1 (which is an end value of 1st for loop ) label 2 will set 2 (which is an end value of 2nd for loop ) and so on ....but i want only 1 for loop and to print 1 to 10 on 10 different labels or on 1 single label
do you have any idea for that ?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an array of JLabels, then in your loop

label[x].setText(""+x);
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is slightly better.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote: is slightly better.




it is showing me null pointer exception ...can you tell me why
i know some where i have not created an object unknowingly and i m using that
can you help me detecting

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's always easier for us to explain exceptions if you tell us where the exception is. The JVM tells you exactly which line it happens on, so pass on the information!

However, in this case you're creating two AppThread objects. You initialise one of them...and call mai() on the other.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will point you where the error is. Hint - Are you initializing and setting text for the same array variable j?

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I only get an ArrayIndexOutOfBoundsException when you try to access j[11]. That's because the array indexes go from 0 to 10.

After I've fixed that I do get a NullPointerException. The reason is simply - you call init() one one instance and mai() on a completely different instance. That second instance never is initialized, and therefore the entire array is still null. The fix is simple:
A better alternative is to replace the init() method with a constructor.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:I will point you where the error is. Hint - Are you initializing and setting text for the same array variable j?


thanks for the exception , but still it is printing only 10 in the JFrame. and not 1 2 3 4 5 6 ...
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you add all the labels in the panel?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:Did you add all the labels in the panel?



thanks mate ..njoyd
reply
    Bookmark Topic Watch Topic
  • New Topic