Paul Clapham wrote:
So first, it extends JPanel so it's a JPanel. And when you create it you initialize it so it contains a JTabbedPane and nothing else (from the GUI point of view, anyway). But then it contains the setTabbedPane and getTabbedPane which you always call like this: setTabbedPane(getTabbedPane()). It looks to me like the purpose of this construct is to add a new tab to the JTabbedPane. So why not just have one method:
Next I looked at your ToolBar class. It extends TabbedPane, which extends JPanel. Having ToolBar extend JPanel is sort of defensible, but having it extend TabbedPane is confusing, because it doesn't need to be a TabbedPane. And both the ToolBar class and the TabbedPane class have a private MainTextArea member, which is a recipe for confusion. It's probable that the error you're asking about stems from this.
However there's this rule of thumb: "Prefer composition over inheritance". In your case that means that a ToolBar object should have a reference to a TabbedPane object, rather than being one. Likewise a MainTextArea object should have a reference to a JScrollPane (in fact, it already does) rather than being a JScrollPane. I'd really recommend that you modify your code to follow that rule, because a lot of code looks like you inherited a class just to avoid creating an object of that class.
Mark Andrew Davis wrote:also could you please advise me on how I can pass reference Toolbar to TabbedPane correctly?
Paul Clapham wrote:
Mark Andrew Davis wrote:also could you please advise me on how I can pass reference Toolbar to TabbedPane correctly?
I'm sorry, I don't understand that. I don't see why a TabbedPane needs a reference to any Toolbar objects.
As for your MainTextArea class, the textArea variable should be private. You have a method which wraps it in a scroll pane and returns it, so there's no need to expose it to the world on its own. I also don't think you need to create and disregard a scroll pane in the constructor.
Mark Andrew Davis wrote:So what I want to do is, in ToolBar class I want to execute the bellow code so it will create a new blank tab. But when I run this I get a nullPointException.
Paul Clapham wrote:
Don't tell that object to request focus. Ask it to request focus.
What I mean by that is, if something owned by a MainTextArea object is supposed to request focus then there should be code in the MainTextArea class which requests focus for it. If you want something outside MainTextArea to ask for that to happen, then there should be a public method in MainTextArea which allows it to ask.
Paul Clapham wrote:
Mark Andrew Davis wrote:So what I want to do is, in ToolBar class I want to execute the bellow code so it will create a new blank tab. But when I run this I get a nullPointException.
You normally get a stack trace when an exception is thrown. It contains all sorts of useful information like the class, method, and line number where the exception occurred. In the case of a NullPointerException the thing to do is to look at the line identified at the top of the stack trace and see what variable unexpectedly had a null value.
Mark Andrew Davis wrote:
Paul Clapham wrote:
Don't tell that object to request focus. Ask it to request focus.
What I mean by that is, if something owned by a MainTextArea object is supposed to request focus then there should be code in the MainTextArea class which requests focus for it. If you want something outside MainTextArea to ask for that to happen, then there should be a public method in MainTextArea which allows it to ask.
Are you able to assist me on how I can achieve this? I've tested a few different ways but I don't think I figure out the code on how to achieve this.
If it's okay could you please explain along with the code so I can learn? I much prefer trying to figure out for myself but I think I am going in circles haha.
Mark Andrew Davis wrote:I am not 100% confident at reading this, but from what I can see is it is stating TollBar class Line 80 has a nullPointException
Paul Clapham wrote:
Mark Andrew Davis wrote:I am not 100% confident at reading this, but from what I can see is it is stating TollBar class Line 80 has a nullPointException
Yes, that's exactly right. And that means that the tabbedPane variable is null.
But if you accept my recommendation about the MainTextArea class, you wouldn't do that. You would write some code which gets a new JTextArea (from a MainTextArea object) and puts it into a scroller and adds that to the tabbed pane and sets the focus to point to it. You could of course put all of that into a method, which would be a good thing, but it would be a method of the class enclosing line 80 and not a method of some other class.
At least I think so. It's always hard to deal with the problem where somebody wrote a ton of code and then realized their object design was all out of whack and needed to be rethought. Which is very common with beginners starting with Swing GUIs.
We don't have time for this. We've gotta save the moon! Or check this out:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|