🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.swing
6 messages
6 total messages Started by Alan Shiers Tue, 21 Dec 2004 20:26
making sense of runtime error
#769
Author: Alan Shiers
Date: Tue, 21 Dec 2004 20:26
95 lines
5227 bytes
Hi there,

I'm working on a project where I'm trying to save to file all the objects
contained in a class that extends JPanel and implements Serializable.  When
I attempt the save using the following code:

FileOutputStream fStream = null;
ObjectOutputStream stream = null;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int result = chooser.showSaveDialog(test.this);
if(result != JFileChooser.APPROVE_OPTION)return;
File chosenFile = chooser.getSelectedFile();
FileOpen = chosenFile.getAbsolutePath();
try
{
    fStream = new FileOutputStream(chosenFile);
    stream = new ObjectOutputStream(fStream);
    stream.writeObject(docPanel);
    stream.close();
    fStream.close();
}
catch(Exception ex)
{
    ex.printStackTrace();
    try{stream.close();fStream.close();}catch(IOException ioe){}
    JOptionPane.showMessageDialog(test.this, "Error: " + ex.toString(),
        "Warning", JOptionPane.WARNING_MESSAGE);
 }

I get the following error at run-time:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.plaf.basic.BasicScrollPaneUI.paint(Unknown Source)
        at javax.swing.plaf.ComponentUI.update(Unknown Source)
        at javax.swing.JComponent.paintComponent(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JViewport.paint(Unknown Source)
        at javax.swing.JComponent.paintChildren(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
        at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
        at javax.swing.JComponent._paintImmediately(Unknown Source)
        at javax.swing.JComponent.paintImmediately(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at
javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow
n Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown
Source)

        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown
Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.plaf.basic.BasicScrollPaneUI.paint(Unknown Source)
        at javax.swing.plaf.ComponentUI.update(Unknown Source)
        at javax.swing.JComponent.paintComponent(Unknown Source)
        at javax.swing.JComponent.paint(Unknown Source)
        at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
        at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
        at javax.swing.JComponent._paintImmediately(Unknown Source)
        at javax.swing.JComponent.paintImmediately(Unknown Source)
        at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at
javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow
n Source)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown
Source)

        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown
Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

There's no reference to any code that I've written in my project, so how am
I suppose to know where to go to fix the problem?

The only thing I understand is somewhere there's a NullPointerException.
But from where does it originate?

Alan
Re: making sense of runtime error
#770
Author: Alan Shiers
Date: Tue, 21 Dec 2004 20:57
7 lines
1138 bytes
Hi,

Just to add some info.  The file save operation appears to be working since
I'm able to open the file and look at it. So, I'm thinking maybe something
else is happening after the save that is causing the NullPointerException?

Alan
Re: making sense of runtime error
#771
Author: Paul Furbacher [TeamB]
Date: Wed, 22 Dec 2004 14:15
54 lines
2569 bytes
Alan Shiers wrote:

> I'm working on a project where I'm trying to save to file all the objects
> contained in a class that extends JPanel and implements Serializable.  When
> I attempt the save using the following code:

Have you stepped through that code in the debugger?
Where does it go off the tracks?

You are trying to serialize an object which
may not exist: is "docPanel" null?

> FileOutputStream fStream = null;
> ObjectOutputStream stream = null;
> [...]
> catch(Exception ex)
> {
>     ex.printStackTrace();
>     try{stream.close();fStream.close();}

In general, check variable for whether they
are not null before invoking methods on them:

     try {
      if ( stream != null )
        stream.close();
      ....

This motif is very commonly used in books
and demos.

A minor point, but you are closing the streams
in two places.

Finally, it's more proper to close them in a
*finally* block.  Again, this is an oft-recurring
motif of a type that should be picked up by
extensive reading of books and demo code.


-- 


Paul Furbacher (TeamB)

   Save time, search the archives:
        http://www.borland.com/newsgroups/ngsearch.html

   Is it in Joi Ellis's Faq-O-Matic?
        http://www.visi.com/~gyles19/fom-serve/cache/1.html

   Finally, please send responses to the newsgroup only.
   That means, do not send email directly to me.
   Thank you.
Re: making sense of runtime error
#772
Author: Alan Shiers
Date: Wed, 22 Dec 2004 16:36
11 lines
1601 bytes
Hi Paul,

Thanks for the advise on the finally block.  I did make that change, but
that doesn't help me with my dilemma.  The problem still exists and I
believe I know why but don't know how to fix it.  When I do this save, the
JFileChooser dialog box appears obscuring the JFrame underneith it. So when
I dismiss it, the JScrollPane (which is part of the JFrame) must be
repainted. It's during this repainting process that it appears to be hanging
on.  How on earth does one fix that?

Alan
Re: making sense of runtime error
#773
Author: Alan Shiers
Date: Wed, 22 Dec 2004 16:53
44 lines
2303 bytes
Hi there,

The JScrollPane is definitely trying to repaint itself, but failing for some
reason.
Just for your further information...my project is not that complicated.  My
main class that extends JFrame declares an instance of JScrollPane at the
top, then loads the JScrollPane in an actionPerformed method like so:

public class test extends JFrame implements ActionListener
{
  JScrollPane sp2 = new JScrollPane();
  DocumentPanel docPanel = null;
  JMenuItem New = new JMenuItem("New");
  ...

  public void actionPerformed(ActionEvent e)
  {
     if(e.getSource() == New)
     {
        Thread newThread = new Thread()
         {
            public void run()
            {
               //Display a new DocumentPanel
               docPanel = new DocumentPanel();
               sp2.getViewport().add(docPanel);
               container.add(sp2,BorderLayout.CENTER);
               container.validate();
            }
         };
       newThread.start();
     }
  }
}
[/code]

docPanel is an object I created that extends JPanel and contains many other
objects.

In the same actionPerformed() method is the code for saving the docPanel
seen in the earlier post.  So, any further ideas as to how to prevent the
NullPointerException I've been getting with the JScrollPane?

Alan
Re: making sense of runtime error
#776
Author: Paul Furbacher [TeamB]
Date: Thu, 23 Dec 2004 04:38
78 lines
3422 bytes
Alan Shiers wrote:

> The JScrollPane is definitely trying to repaint itself, but failing for some
> reason.
> Just for your further information...my project is not that complicated.  

In some sense, it seems to me that you may have made
it needlessly complicated by introducing the threading.
Somehow, you are probably mangling the UI thread, or
something like that with the code shown below.  (I'm
just taking a wild guess because I cannot really make
sense of what you are trying to do, not at least from
my house [a carpenter's saying for "from this great
distance"].)

Maybe you want to do an SwingUtilities.invokeLater
bit of magic here, instead of starting the thread
as you do.  Why is it necessary to introduce
this special threading?

> My
> main class that extends JFrame declares an instance of JScrollPane at the
> top, then loads the JScrollPane in an actionPerformed method like so:
> 
> public class test extends JFrame implements ActionListener
> {
>   JScrollPane sp2 = new JScrollPane();
>   DocumentPanel docPanel = null;
>   JMenuItem New = new JMenuItem("New");
>   ...
> 
>   public void actionPerformed(ActionEvent e)
>   {
>      if(e.getSource() == New)
>      {
>         Thread newThread = new Thread()
>          {
>             public void run()
>             {
>                //Display a new DocumentPanel
>                docPanel = new DocumentPanel();
>                sp2.getViewport().add(docPanel);
>                container.add(sp2,BorderLayout.CENTER);
>                container.validate();
>             }
>          };
>        newThread.start();
>      }
>   }
> }
> [/code]
> 
> docPanel is an object I created that extends JPanel and contains many other
> objects.
> 
> In the same actionPerformed() method is the code for saving the docPanel
> seen in the earlier post.  So, any further ideas as to how to prevent the
> NullPointerException I've been getting with the JScrollPane?
> 
> Alan
> 
> 


-- 


Paul Furbacher (TeamB)

   Save time, search the archives:
        http://www.borland.com/newsgroups/ngsearch.html

   Is it in Joi Ellis's Faq-O-Matic?
        http://www.visi.com/~gyles19/fom-serve/cache/1.html

   Finally, please send responses to the newsgroup only.
   That means, do not send email directly to me.
   Thank you.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads