• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Java Arrays to display String

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Gurus,

Running a simple Java program to display string values :

================Books.java============


==============CallBooks.java===============

=====================================

run:
B1[i].Title
is written by
B1[i].Author B1[i].Title
is written by
B1[i].Author BUILD SUCCESSFUL (total time: 0 seconds)
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It gave you NullPointerException because you haven't create an instance of Book yet. You've just declare a Book typed array that can store 3 items in it.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

creates an array object with length == 3, however the elements are null.

creates an array object with length == 3, and each element is a "Books"

or...

etc

Also, your while loop will execute infinitely (u forgot to increment 'x').
 
Sriram Sreedhar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edited the code above :-


Not sure what i am missing:-

I get the below result .

B1[i].Title
is written by
B1[i].Author B1[i].Title
is written by
B1[i].Author BUILD SUCCESSFUL (total time: 0 seconds)

Any clues ?
 
Sheriff
Posts: 26946
83
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any clues to what? The program is printing out exactly what you told it to print out. You seem to have a question about that, so it would help if you asked it.
 
Sriram Sreedhar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea, Got the results :--

Title Java Complete Reference
is written by Author Herbert Schildt
Title Thinking in Java
is written by Author Bruce Eckel
Title Java for Dummies
is written by Author Barry Burd

Made changes to the code above
 
Sriram Sreedhar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sriram Sreedhar wrote:Java Gurus,

Running a simple Java program to display string values :

================Books.java============


==============CallBooks.java===============

=====================================

run:
Title Java Complete Reference
is written by Author Herbert Schildt
Title Thinking in Java
is written by Author Bruce Eckel
Title Java for Dummies
is written by Author Barry Burd
BUILD SUCCESSFUL (total time: 1 second)

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic