JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You don't show what code is executed when you press the OK button, so who knows?
One guess is that you have for(z=0;z<=counter;z++) so if there are 4 entries (ie counter = 4) you are accessing array elements 0-4 inclusive, ie 5 elements, of which the 5th will still be null.

In general, when yo uget a null pointer exception you should print the exception which will tell you teh exact line where it happened. Then you can print the values used on that line to see which is null, then you should be able to work out why.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

what is my mistake on the line 3-6?

That's been answered more than once already. Maybe time for more thinking, coding, testing between questions?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

" instantiate the array items" doesn't really make sense in Java-speak.
All you need to do is to create/instatiate a new item then put it in the array at the next avaiable slot. It's what you already do on line 5 above, except you need to use your counter to identify the appropriate array index.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

bguild and I are both worried about lines 3-6 in that code. Every time you call addItem you overwrite all five items in the array, thus deleting whatever you added before.

items.length is always better than hard-coding the array size, but in this case you probably should not be doing either. Your array shoud be "big enough" - maybe 100 elements for now, and initially all those elements will be null. As you add new items you will replace those nulls with real items, starting at items[0], then items[1] etc, and keep a counter of how many you have added. Your counter tells you which is the next available element for adding a new item.
Then when you look through the array you use the counter to tell you how many elements to loop through. You don't want to use the complete array size, because then you will be accessing all those null entries that you haven't used yet.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

retrieveItem looks OK, except you have hard-coded 4 as the number of items. You really need a variable that tells you how many items there currently are in the array.
You may want to add some code to check if you went all through the array without finding the id you are looking for - in which ase you could issue an "id not found" error message to the user.

addItem is a bit more of a question... you are adding 1 item, so why add 4 entries to the array? And what is xtr?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

We shouldn't hijack OP's post any more now we have both made our points. I've withdrawn the downvote to help put this to bed.
regards
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi cmps
I haven't seen any relevant code from techxaidz, so I have no idea whether his array is private or not. I know you suggested that, but...
More seriously, he's struggling with which item to get. Showing him how to get an item from its index number really doesn't help. For this to make any sense he needs to retrieve items by ID or title or somesuch, which is the direction I was trying to point him in. Although you can think of this like an ArrayList, I believe it would be more relevant to think of it like a Map<String><Library> where the key is ID or Title.
Having said all that, I'm sure we both agree that this is one of the worst designed and specified exercises ever, and OP deserves all the help he can get.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Is it possible to do this without having a database? (newbie)

Yes, just follow the 3 steps I described. That's all just ordinary Java.

Right now, your "database" is just the array of items. Maybe later you will be asked to create a "real" database to store all that info, but for now it sounds like you are just being asked to hold it in memeory in an array.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's one way to interpret what that method should do...
1. prompt the user for some identifying info (eg item ID)
2. write a loop that accesses each item in the items array in turn, and tests each one to see if they have the ID you are looking for.
3. Once you have found the item in the array you can retrive all its other info and display it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The person who wrote this problem description obviously doesn't understand object oriented design, so that makes it harder for you. Judging from the variables, the class they call Library clearly does not represent a library, it represents one LibraryHolding - which is an abstract class (instance variables ID, Date of Acquisition, Description, number of copies, title) with non-abstract sub-classes Book, Magazine, and Multimedia ( singular nouns, not plural - each instance represents one book etc) that have extra instance variables. There is no class corresponding to a Library - that's just an array of LibraryHoldings.
The addItem and retriveItem methods would sensibly be instance methods of a class that represents a Library, but your teacher has confused that with LibraryItem.
In short - you need a Library class whose instances represents a whole library, and which has instance methods for addItem etc, and an array of LibraryHoldings. And a LibraryHolding class that represents a single item in the library, with variables like ID, title etc.

You really need to make a start on this now. If/when you get stuck post what you have done so far for further help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"but to no avail", while very poetic, tells us nothing. What was wrong with it? Did you set a mono-spaced font eg Courier?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Would this correction fix it?

You should really try that (with the print I previously suggested), then you would know for certain. But, just this once... yes.

Re two matrixes: you look at the values in one matrix, and set the values in the second matrix. Then when you've finished you copy the second matrix over the first (or use the 2 matrices in a flip-flop way, jus swapping the references over after each pass - which saves the copy)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

See, in this part, the ii and jj ints go from i-1 to i+1 and j-1 to j+1.

No, they don't.

for (int ii = i - 1; ii < i + 1; ii++) { // this does not do what you say it should

put a print statement in your loops and see what values of ii and jj you are actually getting.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 54 you include the current cell in your counts, and also the cells diagonally above/left of the current cell. You don't seem to test the cells right/below the current one

ps if (someBoolean == true) is a redundant way to say if (someBoolean)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, the second version is OK too - it's the one I would use because it's clearer than the version with the extra String variable

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Re implementing bguild's excellent suggestion:
Simply create a little class Line, with vars for oldX, oldY etc. Give it a constructor that takes all the values it needs. Declare it as "implements Serializable" so it can be sent over an object stream. "history" can then be an ArrayList of LineData, which can be sent over a socket as a single object.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your class structure looks OK. There's the Bean, which holds info about one (whatever it is), and another class that handles the database access to get the data for a bean, and a GUI class that displays stuff from a bean. That's all good structure.

I'm sure you already know how to return an object from a method. You've done it many times. It's as simple as

public Thing getThing() {
   Thing theThing = new Thing();
   theThing.setBlahBlahBlah... ... ...
   return theThing;
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like you have 2 beans, as I guessed before. One gets populated with values, the other is never populated, so it's all nulls. It's the second one you try to use in your form. Remember that those values belong to each instance of the class, not to the class itself. Every instance has its own values.
You need a single instance of the bean that is populated in Academic then used in MainMenu. One way to do that would be for retrieveAcademic() to return the bean it has created (instead of being void). Then MainMenu can use that bean.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

So what was the result of the print? WHich of the folllowing candidates on that line is the null value?
txt_hours
bn
bn.getHours() // probably this one?

plus (just a guess) is there any connection anywhere between "bean" in Academic (which gets populated wih values) and "bn" in MainMenu (which is where you try to get your values)?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The NullPointerException nessage tells you the exact line where it happened. It's a lot easier if you share that with us! You can also help by printing the values of all the variables and method calls used on that exact line so we all know which one was null.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe its because your layout manager isn't updating to include the new JLabel until you force it by re-sizing the window. Try calling revalidate() (and maybe also repaint()) after adding the JLabel

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

We don't support people who are unwilling to put in some effort.
Look at the SimpleDateFormat doc in the Java API documentation online.
(The method you need is called format, and it's inherited from the DateFormat class).

Just look it up, it's really easy, but nobody here is going to do your homeowrk for you

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Use the SimpleDateFormat class to convert your Date object into a String that's formatted exactly how you want to see it, and use that String in the text field.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Problem is probably that because you are drawing the panel Java has no idea how big it should be. It's probably showing a preferred size of 0x0. It works in the center position because that gets all the available space after the other components have been positioned.Try setting a preferred size and/or minimum size for your paint panel.
Normally people extend JPanel rather than JComponent for this use - that may make a difference as well(?)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

From the API doc...

public void dispose()

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK guys, that's enough repeating each other's posts. Tux4life made it perfectly clear the first time. Please don't just re-post the same answer without adding any new information.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes. I was thinking of class variables, but what you say is exactly right. Thanks for that clarification.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Player player; creates a reference variable that can refer to a Player object, and is initially null.
new Player(p1, p2); creates a Player object
the two things are independent of each other.

player = new Player(p1, p2); creates a Player object and sets player to refer to it, then...
player = new Player(p3, p4); creates another new Player object and sets player to refer to that one.

Once you have declared the variable you can set it to refer to any particular Player object(s) as many times as you want, anywhere you want (provided the player variable is in scope, of course).

It's no different from

String name;      // reference variable (inityially null)
...
name = "Violet";  // set to refer to a String object
...
name = "James";   // or another String object
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

just call setVisible(false); for the first JFrame

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can't mix object I/O and imageIO in a single file like that. ImageIO writes a single image in the desired format (jpg, png etc). The API doc also says " If there is already a File present, its contents are discarded.", so it's stricly one image per file. ObjectStreams support multiple objects in one file, but they have their own special format to do that. Unfortunately BufferedImages are not Serialisable, so you can't write then to an Object stream.
One possible solution is to convert you Image objects to ImageIcon (and v.v after reading them). ImageIcons can be written to /read from Object streams with writeObject and readObject, and you can have as many as you want, mixed with any other objects, in a single file.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Parse: take some data in the form of a string and convert it into an integer or whatever. Java provides standard ,methods for doing that, eg Integer.parseInt(String s) takes a string and interprets it as an integer, and returns that value as an int. It's what nextInt is doing behind the scenes.
Personally I prefer to read all my input with nextLine, then use parseInt, and other methods like it, to interpret the data. That avoids problems like the one you have with nextInt and nextLine. It also works well if you stop using a Scanner and enhance your app with a GUI dialog that prompts for input.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I feel it is a better practice to have only one class where you want to make the changes in

No, not really. That will distort the class structure and force data items into one central class regardless of where they logically belong. Focus on words like "has a", eg a room has a name. That tells you that "name" should probably be an attribute (instance variable) of Room.
In general terms, the "normal" class structure for something like this has a minimum of stuff in Game. Everything about a player is in the Player class, everything about a room is in the Room class, etc etc. The Game class just sets it all up and keeps master lists of Rooms, Players etc, and only handles stuff that doesn't logically fit anywhere else. Tux4life is making the same point with the first 3 items of his last post.

ps Just read your second post that you posted while I was still typing. It's a classic example of what tux & I are saying. A Player has a current location (room). A Player decides to go in some particular direction from the Room he's currently in, as a result of which he gets a new location. That's something that a Player does in conjuction with the info that's in the Room class. There's no reason there why the Game class needs to be involved in that.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You're mixing nextInt and nextLine, which is a bad combination.
nextInt takes the integer value but leaves the end-of-line delimiter (\n). Then nextLine takes everything up to the end of line, is a string of zero length. You need to take or skip the end of line after a nextInt before you take the next "real" line.
Or. Only use nextLine and parse the ints etc yourself.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In this case the answer is trivial. Version 1 works, and version 2 doesn't. Your compiler will tell you the same thing. You can't cast from a String to an int. Check out the Java Language Spec section on casting to see the rules on what you can or cannot convert by a cast.

jalpesh_007 commented: thanks.. +4
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Matt
Two things you may have misunderstood...
1. This is not a "we do your homework" servce. Somebody may help you learn how to round your results, but nobody will just do it for you.
2. This is the Java forum. Java and JavaScript are not the same thing at all.

So: Please post a request for assistance in learning how to round values, and post it in the Web Development / JavaScript forum

Good luck
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It woiuld be nice if uou could use that syntax in Java, but you can't. It's not part of thelanguage.
-13683360 <= rgb evaluates to a boolean (true/false)
and then
(true/false) <= -13683381 is a bad operand because you cant't use <= with a boolean.
You need to break it into two comparisons the AND the reulsts together
(-13683360 <= rgb) && (rgb <= -13683381)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I completely disagree with the use of public variables. They should be private and have methods that access them.

I agree absolutely (with the exception of static final public, of course)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No 3..

    interface A {void x();}
    interface B extends A {void y();}

just try it.

No 4. Depends on whether the class is defined as abstract or not (question is not clear)

No 8. Java Language Spec says "This (multiple) interface inheritance allows objects to support (multiple) common behaviors without sharing any implementation."

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, I guesed that was what you meant, although when people do say "global" they usually mean "public" But because most of the people reading these posts are early on the Java learning curve you should stick to standard Java terminology. There's no such thing as "global" in the Java language. (In fact there's only one use of the phrase "global variable" in the whole Language Spec, and that's an informal use in a discussion of memory models for multi-threading in the VM.)
It's just a matter of avoiding confusion for readers who may be already pretty confused about Java ;)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi RockJake - eould you like to define "global variables" for those of us who speak Java? ;)
How about initialisation in their declaration, or in an instance initialiser block? Both those lead to simpler safer code when there are multiple contructors.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There is an overlap in function between constructors and setters, and no rigid rules about which to use. But some typical guidlines include:

  • If a variable must be set for an instance to be valid (eg Person must have a name and social security number) then set them in the constructor. That way there's no risk of an invalid instance because someone forgot to set something.
  • If it's optional to set a variable then an ordinary setter is better.
  • If a value can change you must have a setter, even if it is also set in the constructor.

Don't forget also that you can have multiple constructors, each taking a different number of parameters, so you can use which ever constructor lets you set exactly those values that you know when creating the new instance.

In the end this is mostly a question of style and preference, so I wouldn't get too hung up on it.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's called an anonymous inner class, it defines a new subclass of Handler and creates an instance of it. Google Java anonymous inner class for tutorials etc.

hamidvosugh commented: Very helpful. Thank you. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Perhasps use Regex instances to define each of the variants/replacements you are looking for?
You could place all the current Regex's in an ArrayList and loop thru them regardless of how nany there are.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Remember that this code is just one tiny training exercise. In real life there's no such thing as a program as small and useless as this one. In real life the use of getters and setters is an essential part of Java OO programming technique, and it's never too early to start learning and practising their use.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thta's not a whole program, it's just one class. That class will be combined with other classes to make a useful program. The other classes will then use the getters to access the Employee info.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
   byte[] array = "1".getBytes();
   System.out.println (Arrays.toString(array));

Will give the output

   [49]

ie an array of one element containing the decimal value 49 - which is the UniCode (and ASCII) code for the character '1'

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's the default output created by the toString method that every object and array inherits from Object. It means

[    this is an array
B    of bytes
44770739   and that is its unique hash value

(very precise, and totally useless!)

To convert an array to a useful printable string use the toString method from the Arrays class, eg
System.out.println (Arrays.toString(array));

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The wikipedia article on quadratic probing starts with a really clear and simple explanation...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's great! Please consider marking this thread as "solved" so people will know there is an answer here.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I can't see where you have added the action listener to the buttons. It would be easy to do that in the Buttons constructor.
http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#addActionListener%28java.awt.event.ActionListener%29