Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java - Exceptions.

Options
  • 23-02-2007 7:10pm
    #1
    Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭


    Hey all.

    OK well im doing BSc App. Computing (second year) and our main programing language is Java. Recently we done a Football League assignment (adding teams, playing matches, having fixtures, and sorting) which didnt go to badly. Found the sorting difficult tho.

    So I have decided to work on another similar style project (Top 20 Chart!) for my own benefit to help my knowledge in Java (which isnt great) - and I have encountered some problems (a lot of the coding has been done) with "exceptions" which we never covered in a lecture so I cant really understand nor get myself out of the mess. So I was hoping somebody here would be kind enough to explain why I am encountering the problem, and how I should go about to fix it. Im baffled to why these problems are appearing, as they didnt appear in my Football League assignment which this is based of.

    Im not looking for a straight "heres the new code" solution, im hoping that somebody can help me understand what the problem is, why it occurs and what I need to do to prevent them and fix them if they occur. I use the Eclipse editor to code.

    Firstly; When I try run the "Console" class as a "Java Application", it spits out errors at the very start;
    java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at top20Chart.Console.readChart(Console.java:103)
    at top20Chart.Console.<init>(Console.java:16)
    at top20Chart.Console.main(Console.java:175)

    From what I can gather, the code seems to be hassled by the Input / Output code which our lecturer gave for the football league. It deals with taking inputs from the console. Strangley, this code was not modified when moved over from the Football league - so I have no idea why it has decided not to work. From my brief knowledge of exceptions - are they not methods which deal with errors that happen in the code? So they would be developed by the coder?

    The next error a "null pointer exceptions" happens when I try to display all artists that I have added (I have options for Adding / Editing and Removing artists).
    Exception in thread "main" java.lang.NullPointerException
    at top20Chart.Artists.displayArtist(Artists.java:116)
    at top20Chart.Console.doMenu(Console.java:151)
    at top20Chart.Console.main(Console.java:176)

    Problem seems local with my displayArtist method, which is a loop for running through my LList "artistsList" and calls the display() method when complete - which prints the results to the console along with some text. Cant see what iv done wrong however.

    Iv attached the appropriate code sniplets, so as to give people an idea of how I have developed part of the application so far. Hopefully it will show to you the problems, and you can explain what iv done wrong - so I can go about fixing them.

    Thanks in advance for your help on this.


Comments

  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    Sully04 wrote:
    The next error a "null pointer exceptions" happens when I try to display all artists that I have added (I have options for Adding / Editing and Removing artists).

    Problem seems local with my displayArtist method, which is a loop for running through my LList "artistsList" and calls the display() method when complete - which prints the results to the console along with some text. Cant see what iv done wrong however.
    OK I've had a really quick look at the code and the ArtistList exception and the "null pointer exceptions" that is thrown there is usually thrown when iterating over lists incorrectly i.e. when you iterate over the list more times then there are elements in the list. This means the last iteration will trow an exception because you are referencing something which doesn't exist. So you are calling a pointer to something that isn't there hence the name "null pointer exceptions" So at a quick guess change the code displayArtist to [PHP]public void displayArtist() {
    for (int index = 1; index < artistsList.getLength(); index++) {
    artistsList.getEntry(index);
    display();
    }
    }[/PHP]
    Thereby removing the last iteration of the list. That might fix that.

    As for the other exception "java.io.EOFException" I think that stands for EndOfFileException therefore the java VM is complaining about the termination used in the file that you are handing in for the code to process. Is the file an object saved as a dat or just a text file?


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Hi,

    Im going to try your solution shortly. You said that you removed the last iteration from the list - but you only changed the <= sign to become < ? Whats the big difference, it just doesnt check if its equal?

    The file is a dat file which the data is stored to. Termination is done by selection "0" on the menu, which runs this method;

    [php]public void exit() {
    this.writeChart();
    System.exit(1);
    }[/php]

    which exectues this method (which I wouldnt understand, as it wasnt really ever explained):-

    [php]public boolean writeChart() {
    boolean result = true;

    try {
    ObjectOutputStream output = new ObjectOutputStream(
    new FileOutputStream(fileName));
    output.writeObject(chart);
    output.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    result = false;
    } catch (Exception ex) {
    ex.printStackTrace();
    result = false;
    }
    return result;
    }[/php]

    which hasnt changed since the Football League (where it worked).

    Thanks again


  • Closed Accounts Posts: 3,285 ✭✭✭Smellyirishman


    Sully04 wrote:
    Im going to try your solution shortly. You said that you removed the last iteration from the list - but you only changed the <= sign to become < ? Whats the big difference, it just doesnt check if its equal?

    Let's say this is your artistList.
    [0]	[1]	[2]	
    Eminem	Beyonce	Metallica
    

    So, at index [0] the value stored is "Eminem".

    So our artistList.length = 3

    Previously, you were taking the values from array indexes 1,2 and 3. Since 3 is <= length. However, as you can see, there is no index 3 as indexing starts at 0. That is why you were getting the null pointer exception, because you were trying to reference element 3 and it didn't exist. It should also be clear, that you are currently skipping the initial element in your list since you are starting at int index = 1;.

    So, while you don't get a compiler error atm, you will print out only element 1 and element 2.

    But we want all 3, so finally, the loop should read like this.

    for (int index = 0; index < artistsList.getLength(); index++) {

    As you can see, we go from
    0 < 3 print Eminem
    1 < 3 print Beyonce
    2 < 3 print Metallica
    Stop.

    Printing all elements, and preventing a pointer error.

    What data structure are you using to hold the artists? An array? (I don't recognise the getEntry method)


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Thanks for that. Just a quick question, do all lists start at 0 (its been a while since I covered this)? I cant understand why I had my code to start indexing at 1, and not 0 earlier.

    Its an LList, in our dataStructures project we done earlier on in the year. Im just importing the stuff from that, and using that method. Ill clear this up in a bit, I need to take a look at that package to refresh my memory.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    The changes to displayArtist() method make no difference - the error still occurs. :/


  • Advertisement
  • Closed Accounts Posts: 3,285 ✭✭✭Smellyirishman


    Try changing the initial index back to one then. If you have written the Linked List yourself then it may be that the first element is indexed at 1. Sorry for the confusion, but I wasn't sure what type of structure you were using. Although if that is the case, then <= to the length should work. Are you putting something into the list before trying to print the elements? Are you sure that the Linked list is working OK?


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    I added an item to the list, and then called the display method to print out what was in the list. The only errors encountered are when I first execute the program, and when I run the display method.

    I have only very briefly touched on JUnit Testing in 1st Year, and would not be experienced enough to test it out that way. Can you suggest another way of seeing if the LList is working correctly?


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    OK well iv just done some testing, and while I dont get any errors when adding an artist - it doesnt write what I added to the chart.dat file. I dont see why not, as iv made the class serilizable, told it the filename is chart.dat, and wrote to the list from what I can see. Now, I might have missed out on something very obvious - so iv attached the source for Artists.

    That would explain the null pointer - if data isnt being written, you can expect to get data when calling display. Question is, why isnt it writting the data - and not giving any errors.


  • Registered Users Posts: 4,188 ✭✭✭pH


    Without the full code (including these 'Console' classes that lecturers seem to love so much and LList) it's very hard to help you much.

    However there does seem to be some confusion in what your 'Artists' class actually represents. It has private fields for name, nationality etc. but also includes a private that appears to be a linked list called artistslist.

    It's not clear to me whether the Artists object represents a collection of artists (in which case there should really be another class Artist that represents a single artist) or if it represents an individual artist (in which case each individual artist shouldn't contain a linked list of artists).

    Unless of course the design represents something altogether different that I've missed.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    I have given the full code for Console and Artists class. Iv only one other class called Chart, but it doesnt really connect with my code problem.

    The idea behind the Console class was to have the Main Menu and the Input reading code provided by the lecturer.

    The idea behind Artists class was to hold the information on many artists (its a top20 chart, so we can add 20 artists). The information such as nationality, name, solo, band etc.

    The idea behind Chart (not really mentioned) class was to contain methods and lists for both the Singles & Album chart. Its very similar style to Artist class (methods wise).


  • Advertisement
  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    Sully04 wrote:
    The idea behind Artists class was to hold the information on many artists (its a top20 chart, so we can add 20 artists). The information such as nationality, name, solo, band etc..
    No offense but I don't think that's the best way of implementing that; you should make an Artist class and create a vector or list of artists. I realize this may appear to be pointless but the better laid out your code is the easier it is to understand and therefore to debug. Objects should be singular instances that way it's much easier to unit test them in the basic "output to console"-method.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    None taken, this is just the approach we were advised to take. Never done vectors (in programing). I dont see what the difference is, I have an Artists class with a list of artists (well, thats what I was aiming for but it seems my code was incomplete so I have revised it and got rid of the EOF error but the null pointer occurs when I call the addArtist() method.)

    Iv revised my code, and attached a dataStructures project with an LList class (i know there are errors with some methods, but I dont use them yet).

    (I may have uploaded older code earlier, not to sure).


  • Registered Users Posts: 4,188 ✭✭✭pH


    Fundamentally you are still trying to use the single class 'Artists' as both storage for a single artist and also to store the list of artists.

    If you made your LinkedList static you might be able to get something working but it's still not a great idea, much better to have a class that represents just an artist.

    Also as an aside - Java 1.5 has been out for a while, why are students not being taught to use Generics, and remove all those ugly casts?
    Artists tArtist = (Artists) artistsList.getEntry(index);
    

    And why oh why can't you use an Iterator to traverse the linked list!


  • Registered Users Posts: 233 ✭✭beanhead


    as for sorting, read up on the comparator interface


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    pH wrote:
    Fundamentally you are still trying to use the single class 'Artists' as both storage for a single artist and also to store the list of artists.

    If you made your LinkedList static you might be able to get something working but it's still not a great idea, much better to have a class that represents just an artist.

    OK. So ill create an Artist and Artists class. Artists having the LList (cant think what else) and Artist having the methods for adding, removing, editing, displaying etc.

    Going about that the right way?
    Also as an aside - Java 1.5 has been out for a while, why are students not being taught to use Generics, and remove all those ugly casts?
    Artists tArtist = (Artists) artistsList.getEntry(index);
    

    And why oh why can't you use an Iterator to traverse the linked list!

    Not to sure what your saying here.

    Sorry, im not that good with programming so bare with me! Cheers. :)


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Well with help from a friend, I managed to get this working.

    I switched my LList to ArrayLists. Also created seperate classes as recommended here, for Artist, Album and Single. So that got adding/editing/removing of aritsts/singles/albums done.

    Also managed to fix up the loop so that it displayed only 20 albums / singles at any one time. Managed to get one sorting option - sort the charts by default of weeklySales.

    So it works fine without any errors. :) Just thought id give ye an update. Cheers for all your help :)


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Well with help from a friend, I managed to get this working.

    I switched my LList to ArrayLists. Also created seperate classes as recommended here, for Artist, Album and Single. So that got adding/editing/removing of aritsts/singles/albums done.

    Also managed to fix up the loop so that it displayed only 20 albums / singles at any one time. Managed to get one sorting option - sort the charts by default of weeklySales.

    So it works fine without any errors. :) Just thought id give ye an update. Cheers for all your help :)


Advertisement