• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextArea From Another Class  RSS feed

 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I am trying to display some text from another class, but for some reason when I call the method it doesn't work.

I am trying to call the setMainTextArea method from the ChapterOne class, but I get and error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.cagatandavis.ChapterOne.<init>(ChapterOne.java:16)

Here is my relevant code:









Any assistance would be wonderful! I thought I could call mainTextScreen.setMainTextArea(""); from another class and it would work. But I might be either missing something or miss understanding something.
Any assistance would be wonderful! Thanks
Mark.D
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only use com.something in package names if you own that website address.
I cannot tell what is null because the line number (16) doesn't exist in the class you showed. By the way, ChapterOne is a very poor name for a class not representing a chapter.
You are declaring a field, but not initialising it, so it will be null.
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Only use com.something in package names if you own that website address.

Yep, I own this domain which is why I am using it.

I cannot tell what is null because the line number (16) doesn't exist in the class you showed.

Sorry, for some reason I pasted the old error. Here is the current error:


So it would be  line 13 on chapterOne.

By the way, ChapterOne is a very poor name for a class not representing a chapter.
You are declaring a field, but not initialising it, so it will be null

Sorry, I don't really get what you mean? could you please elaborate? Sorry I am still new and learning.
Should I name it something else? Since this is a text based game with still Images, I was going to have each chapter tell the story so class ChapterOne tells the story for chapter one and displays it onto the main text area.

Just to confirm, when I say private MainTextScreen mainTextScreen; this is declaring and when I use mainTextScreen.setMainTextArea("TestOutput"); this is initialising it, is it not?

Sorry for all the questions!! But I do greatly appreciate your time and assistance ^_^

Thanks,
Mark.D
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have uploaded all my code to here if you wanted to review. I didn't add all the classes to the thread.

https://github.com/markcagatandavis/geek-in-a-rice-grain
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I have initialised the mainTextScreen and now I am no longer getting the error.

My issue is now, I can't see the text in the JTextArea? :/

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Andrew Davis wrote:. . . I own this domain . . .
If I forget and query it again, you are allowed to tell me off.
You are declaring a field, but not initialising it, so it will be null
Sorry, I don't really get what you mean? . . .
The Java® Language Specification (=JLS) tells you that field variables are initialised to default values before they can be used. The default value is some sort of zero and in the case of a reference type, that corresponds to null. The rules are different for local variables and final fields. That means the field to be used for your List points to null as a default. As you doubtless know, you are very restricted about what use you might want to make of null; one thing you cannot do is put it to the left of the dot operator. So whenever you have something pointing to null and try to call its methods or anything like that, you uffer an Exception.
The answer is not to try to handle the Exception, nor to make any tests about whether it is or isn't null. The answer is to get rid of the null. And the way to do that is to write the code such that your List reference always points to a List object. You want to initialise that reference in the constructor (or earlier). If you haven't initialised it by the time the constructor completes, you are risking having some code or other trying to use it when it is still null, so make sure you initialise it. I like constructors:-but there are other ways to do it.

This isn't a games specific question but a basic question so I shall duplicate this discussion where we discuss things basic.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was getting all confused, thinking it was a List you hadn't initialised when it was a text area. The same principles apply to all reference types, and you have found a different way to initialise it. Well done.
I think we shall need a bit more information about which class the text area you are having problems with is in. You may find that explaining all that enables you to find the solution for yourself.
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help I have gotten most of what I am trying to achieve working now!!

As stated before but, the only issue I am having now is that the JTextArea is not updating if I send text from the ChapterOne class to the setMainTextArea Method in the MainTextArea Class.

Here is my code:






Here is the breakdown:

I have the JTextArea in the MainTextScreen and I want to be able to have text set from "ChapterOne" Class and display on the JTextArea in the MainTextScreen class.
For this, I am using the method setMainTextArea in the MainTextScreen Class which should call the text and then spit it out to the mainTextArea, but it's not spitting the text out.

I have ran a System.out.println(); on the setMainTextArea method and can confirm the text from ChapterOne is being called and displaying in console.
This has lead me to believe the JTextArea is not refreshing or somewhere I am calling a new instance of the JTextArea?

I have test with .revalidate(); and .repaint(); in the setMainTextArea method and still to no avail.

I also have a JTextField in another class which points to setMainTextArea method and I can confirm that if I add text into the JTextField, it does display on the JTextArea.


In case you need it, here is the code for the player Input field for the JTextField:



From my understanding the PlayerInputScreen class shouldn't cause the issue I am having as I have created everything segregated from each other (from my understanding)
but I thought I would highlight this in case I am over looking something.

I hope all this makes sense and thank you in advanced for your assistance.
Mark.D

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and how many MainTextScreen objects do you have? How are you adding them to a frame? How are you making them visible? How do you know it is the same object as in the chapter one object?
Your setMainTextArea method is named incorreclty; it shoul‍d be something like appendTextToTextArea, because that is what it does. But I am getting suspicious about methods with such long names. It looks as if you had another design problem hiding somewhere.
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:. . . and how many MainTextScreen objects do you have? How are you adding them to a frame? How are you making them visible? How do you know it is the same object as in the chapter one object?
Your setMainTextArea method is named incorreclty; it shoul‍d be something like appendTextToTextArea, because that is what it does. But I am getting suspicious about methods with such long names. It looks as if you had another design problem hiding somewhere.



Sorry for the late reply, I have been really busy lately.

I have the MainTextScreen class and a MainTextScreen object in the ChapterOne class. I am adding the MaintextScreen to the IngameFrame class as followed:




I have changed my setMainTextArea to appendMainTextArea.

So on my appendMainTextArea I have performed a System.out.println("TEST"); which works if I call appendMainTextArea from ChapterOne

    public void appendMainTextArea(String text)
    {
        mainTextArea.append(text + "\n");
        System.out.println("Test");
    }


I have also tested appendMainTextArea as a public static void for testing purposes and confirmed appendMainTextArea displays text to the MainTextArea if it is public static.


I have hit a stump on this one... really unsure on the troubleshooting I need to perform to properly diagnose this...
I have tested with both repaint(); and revalidate(); in the appendMainTextArea in which to no avail. The only think I can think of is, the MainTextScreen(); object I have in ChapterOne is creating a new instance which is not displaying?? :/

I believe the way I am doing it will create a new instance of MainTextScreen, which is not what I want. I want to add to an existing instance.



for testing purposes I even did the following: which didn't work either



Any input would be wonderful Thank you for your assistance thus far.
Mark.D
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Andrew Davis wrote:. . .
Don't. Always use a layout manager and let that sort out the sizes of Components.
Why? Where are you using that layout?
. . .
. . .
That looks complicated, and things that complicated are likely to go wrong. At least it is less complicated than the anonymous class you had before. Where does that listener class come from, and the event class? Are you using methods to set the text or append to it? How do you know what text that event object is carrying?
I have also tested appendMainTextArea as a public static void for testing purposes and confirmed appendMainTextArea displays text to the MainTextArea if it is public static.
So, how do you know whether that method is being called at all?
. . . I believe the way I am doing it will create a new instance of MainTextScreen, which is not what I want. . . .
No, it isn't, I don't think.

You need to work out where you have references to that screen in the other object. I think you shou‍ld try a cut‑down version of the display and find that screen instance and work out how to make the other display update it. Then gradually add back the parts you removed.
 
Mark Andrew Davis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
You need to work out where you have references to that screen in the other object. I think you shou‍ld try a cut‑down version of the display and find that screen instance and work out how to make the other display update it. Then gradually add back the parts you removed.


Thanks so much for your advise!!! This helped so much!!

Turns out I wasn't passing it through correctly... but because I cut back on a lot of things, it allowed me to scope down my troubleshooting.

In the ChapterOne class I passed MainTextScreen as followed:


And in the MainTextScreen, I cut back the constructor to its bare minimum


I'm a little bit confused, because I am still learning so if you are able too can you please explain to me/break down to me what I did?

From my understanding:

- In the chapterOne class I pass through the MainTextScreen with private MainTextScreen maintextScreen;
- Then when I do public ChapterOne(MainTextScreen mainTextScreen){} I am creating a new object called mainTextScreen ?? (this is the part I don't understand)
- then this.mainTextScreen = new mainTextScreen; that is initialising the instance I created at the start and passing through what ever mainTextScreen is equal too through to the appendMainTextArea constructor?

Is that correct?

I tried looking this up, but I couldn't find or isolate the core concept well enough to try and truly understand what I just did.

Any references or even advise on the concept I just used to go back and study would be greatly appreciated!!

Thought I would include a screenshot of what I am making
- Left top corner is a novel that tells the story
- Bottom left corner is a player input field that allows you to direct the story via player choices
- Top right will be a graphics area that displays still motion images about the story
- Bottom right hand corner is the player and enemy battle field stats in live combat

Thanks for your help

Capture.PNG
[Thumbnail for Capture.PNG]
Game ScreenShot
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Success at last Well done.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!