• Can anybody see an error in my code?
    17 replies, posted
Hey. I have this assignment due, and basically it is to do a bookstore sort of thing. There is different levels to it etc etc. I have some code that compiles fine, but I keep getting a NullPointerException on trying to run it. Here are some things about the code. 1) There are 2 external files, a binary file containing Author, Title & ISBN number. Another is a text file containing ISBN numbers and a stock level number. 2) The level of the code I am trying to do asks me to make it so that it reads in these, in separate methods, and then check the ISBN's from the text file, and replace these with the corresponding title of the book. I had this program perfectly printing out everything fine, but since the 3rd level of the assignment requires me to replace the ISBN with the title, I had to try different things. I cannot get this working, as I am trying to read in a book one at a time and then compare the ISBN's but it won't work. Would appreciate any help. Here is the code: [url]http://pastebin.com/0QCf3wzy[/url]
cant see anything sorry
Hmm, my friend here couldn't see anything either. I have no idea why it isn't working.
The reason it doesn't work is that when printBook() runs for the first time, the value of s is zero, so the while loop doesn't run, therefore getBook() isn't called, therefore the isbn1 variable (among others) never gets set to a value. The exception occurs in changeISBN(), where it tries to compare isbn1 to isbn2, because isbn1 is null. This is the sort of thing you can figure out in a few minutes by stepping through the program with a debugger.
Wyzard is my hero. My assumption is that the OP only knows Java, and so debugging tools aren't quite as necessary when first starting out. But once you hit c/c++ gdb and valgrind become essentials (at this level of coding). Some day, my friend.
Yeah your right I only know Java right now (1st year in college) How would I go about fixing this do you know?
assign s a value
What value though? I tried set it a the value in.length(); but in is in a method so I don't really know what to do with it :S
It looks like s is meant to be the total length of the books.dat file, and i is meant to represent the portion that's been read so far. Each call to getBook() reads the "next" book from the file, and the "while (i < s)" loop in printBook() is meant to loop over all the books in the file. The i and s variables are only set (in getBook()) [I]after[/I] you've decided to read another book, but you're using them printBook() to decide [I]whether[/I] to read another book. That's the fundamental logical flaw you're running into. getBook() is intended to be called multiple times if there are multiple books in the file, so the fact that every getBook() call opens the file and checks its length is a sign that those steps probably don't belong in that method. You should open books.dat and check its length once, [I]before[/I] starting the "while (i < s)" loop in printBook(). That way, s can have a correct value prior to that first comparison. I'd comment on the transaction-related stuff in the second half of the program, but it's not clear to me what it's supposed to do. It reads only the first ISBN from transactions.txt, and compares it only against what would be the last ISBN from books.dat if getBook() and printBook() worked correctly. Based on the names of the methods, calling changeISBN() from getTrans() is confusing (I wouldn't expect a "get" method to modify the data it's getting), and the fact that changeISBN() just prints a message and doesn't actually change anything is also confusing. I assume this portion of the program is unfinished.
In addition, I don't think you get OOP, every class variable you have there does not need to be a class variable at all (can all be declared inside the functions), which in the end means your entire class is just a collection of functions. Maybe you could have a Book class (which has your isbn, title, etc), and then your store is a list of books, I can give an example if you want, cause I'm bored, and haven't done anything in Java in so long. Also, in regards to Wyzard's last paragraph, you definitely have some problems with style, but won't affect the actual running of your program.
Ah I get what you mean. I had the books.dat printing perfectly, and I also had the transactions.txt printing underneath perfectly I.E 12356 Jan Skatson Java for Beginners 100 There would be like 6 more of these with different things there and underneath this would be the transactions.txt which was like 123567 -55 So basically what I had to do was compare the first line of numbers in the transactions to the 1st line in book and then replace it with the title of the book to the corrosponding ISBN. I hope that helps clear up what I am trying to do! I just don't know how to replace the ISBN with the title, so yeah I and S were going to be used to see if they were the same or not, and if not the same to move on.
When you say "replace ISBN with the title", do you mean printing out the contents of transactions.txt, but with the title in place of the ISBN field? I [i]think[/i] that's what you mean, but you haven't actually said that you're supposed to print the transactions, and it could (instead) mean actually modifying the transactions.txt file on disk. Anyway, to look up the title associated with a given ISBN, you'll probably want to use a java.util.Map. (That's an interface; HashMap is the typical implementation to use.) You can populate the map with ISBN/title entries while reading books.dat, and then use it to look up titles by ISBN while reading transactions.txt. [QUOTE=yumi_cheese;29486270]Maybe you could have a Book class (which has your isbn, title, etc), and then your store is a list of books[/QUOTE] I agree, that'd be a good idea. [QUOTE=yumi_cheese;29486270]I can give an example if you want, cause I'm bored, and haven't done anything in Java in so long.[/QUOTE] This looks like an assignment for a class, so you shouldn't be giving him code. [editline]28th April 2011[/editline] [QUOTE=Andy;29486321]so yeah I and S were going to be used to see if they were the same or not, and if not the same to move on.[/QUOTE] That much is obvious from the code. What's important is why it [i]matters[/i] whether they're the same or not. You're using these variables to determine whether you've reached the end of the file while reading it.
well, I did go ahead and write up the code for your project, and it ended up being a good refresher for java, which I haven't used in about 2-3 years! :O However, at Wyzard's advice, I won't give you the full code, but I'll give you a bare skeleton of the code, with the function names and what they should do. It's not even the full functionality because I was not sure what you were trying to do with get trans... So it is just for the part that prints the catalogue of books. Here it is: [url]http://pastebin.com/svNiP4bD[/url] I take no responsibility if there are any mistakes :P.
[QUOTE=Wyzard;29486510]When you say "replace ISBN with the title", do you mean printing out the contents of transactions.txt, but with the title in place of the ISBN field? I [i]think[/i] that's what you mean, but you haven't actually said that you're supposed to print the transactions, and it could (instead) mean actually modifying the transactions.txt file on disk. Anyway, to look up the title associated with a given ISBN, you'll probably want to use a java.util.Map. (That's an interface; HashMap is the typical implementation to use.) You can populate the map with ISBN/title entries while reading books.dat, and then use it to look up titles by ISBN while reading transactions.txt. I agree, that'd be a good idea. This looks like an assignment for a class, so you shouldn't be giving him code. [editline]28th April 2011[/editline] That much is obvious from the code. What's important is why it [i]matters[/i] whether they're the same or not. You're using these variables to determine whether you've reached the end of the file while reading it.[/QUOTE] Basically what I mean is that the book data is printed out on the top, and then for level 2 of the assignment we print the transaction.txt file below it, which contains all the isbns and stock levels. For Level 3 of the assignment, we replace the ISBNS from the transactions.txt with the title of the book. So we have to read the title from the binary file and replace it with a long int from a txt file. Really appreciate your help mate [editline]28th April 2011[/editline] [QUOTE=yumi_cheese;29486882]well, I did go ahead and write up the code for your project, and it ended up being a good refresher for java, which I haven't used in about 2-3 years! :O However, at Wyzard's advice, I won't give you the full code, but I'll give you a bare skeleton of the code, with the function names and what they should do. It's not even the full functionality because I was not sure what you were trying to do with get trans... So it is just for the part that prints the catalogue of books. Here it is: [url]http://pastebin.com/svNiP4bD[/url] I take no responsibility if there are any mistakes :P.[/QUOTE] Your a star mate I will have a look. And yeah smart thinking, its a lot better to not give the full code so I can try get my brain thinking. Cheers for the help will try it now
[QUOTE=Andy;29491997]For Level 3 of the assignment, we replace the ISBNS from the transactions.txt with the title of the book. So we have to read the title from the binary file and replace it with a long int from a txt file.[/QUOTE] You wrote this sentence as if it's a clarification, but it's just a repeat what you said before. Replace it [i]where?[/i] Is your program supposed to modify the transactions.txt file?
Well I got the program working with help from a mate IRL. Got thew level 4 done which is worth 90%. It doesn't actually change the .txt file it just uses data in it and changes that, but it isn't saved into the txt file. Here is the code for what I used it might explain it a bit better. Sorry for being a bit confusing but I really appreciate the help mate. [url]http://pastebin.com/sCGdr0Dd[/url]
This is basic and requesting help for a uni assignment defeats the purpose of university!
[QUOTE=Ed Miliband;29511060]This is basic and requesting help for a uni assignment defeats the purpose of university![/QUOTE] Don't be a tool, yes it's basic, he's just learning. The purpose of university is to learn, which is hard to do without some assistance. The code itself is still quite bad in bits, and you're still making variables class variables when they ought not be. You know you can declare variables within functions right? Or really anywhere. Most of the time you can keep class variables to what that class has. For instance a store might have books, and a name, and an address. But a store doesn't have an isbn title stocklevel etc. These variables pertain to books, and even though you might want to store them temporarily so you can gather them before creating a book, they don't need to be there in the class. Declare them at the top of your Store function. As another note public Store() is a constructor, it shouldn't be public void Store(). [url=http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29]This is what a constructor is[/url] and [url=http://www.javabeginner.com/learn-java/java-constructors]this is how they are used in Java[/url]. Essentially a constructor is run whenever you create an object, by default, you have the default constructor (which basically just creates the object and leaves everything empty), but you can specify your own. This would mean down in your main function, you're just have Store foo = new Store(), this calls the constructor (which in this case is your function public void Store(), but you need to remove the void). Alternatively you could just change the Store function to something better named like Initialize or whatever.
Sorry, you need to Log In to post a reply to this thread.