Home arrow static arrow Java Programming [Archive] - Instance NOT an instance of an object
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Instance NOT an instance of an object
This topic has 9 replies on 1 page.

Posts:668
Registered: 9/2/03
Instance NOT an instance of an object  
Aug 6, 2004 10:13 AM



 
Hey,

Any ideas on how to check if an instance is not an instance of a particular object?
Obviously, this doesn't work:
        if (objType == null || objType !instanceof File                || !(objType !instanceof URL)) {            // if type of object was a nullpointer,            // or not a file or a URL play system beep.            Toolkit.getDefaultToolkit().beep();            return;        }
 

Posts:37,103
Registered: 3/30/99
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:15 AM (reply 1 of 9)



 
if (!(obj instanceof Foo)) 

You don't even need to check for null, as null instanceof anything always evals to false.
 

Posts:668
Registered: 9/2/03
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:20 AM (reply 2 of 9)



 
I've even tried that example before posting :) Perhaps my situation I am in will enlighten things up.
Snippet:
    /**     * Plays a sound.     *      * @param file     */    public void playSound(File file) {        playSound((Object) file);    }     /**     * @param url     */    public void playSound(URL url) {        playSound((Object) url);    }     /**     * Plays a sound. Should only be used in this class, internally.     *      * @param objType A File object or an URL object.     */    private void playSound(Object objType) {         // if the objType was not an instance of a file or an URL, play a normal system beep.         if (objType == null || objType instanceof File                || !(objType instanceof URL)) {            Toolkit.getDefaultToolkit().beep();            return;        }         // if i call playSound(URL), or playSound(File), it doesnt reach this point.        // even though System.out.println's say that the objType is not null, and        // is in fact a URL or File.                 try {            // From file            AudioInputStream stream;            if (objType instanceof File) {                stream = AudioSystem.getAudioInputStream((File) objType);            } else { // if it isn't a file instance, it's an URL.                // From URL                stream = AudioSystem.getAudioInputStream((URL) objType);            }             // ...            // blah blah blah, some encoding done here and all blahdy blah            // ...             } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } catch (LineUnavailableException e) {            e.printStackTrace();        } catch (UnsupportedAudioFileException e) {            e.printStackTrace();        }    }

Perhaps I am being an idiot like always and I am way off track (as usual as well).
Enlighten me! :P
 

Posts:37,103
Registered: 3/30/99
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:25 AM (reply 3 of 9)



 
// if the objType was not an instance of a file or anURL, play a normal system beep. if (objType == null || objType instanceof File|| !(objType instanceof URL)) {Toolkit.getDefaultToolkit().beep();return;}

Your comment here doesn't match your code. Comment says "if it's neither file nor URL..." but code says "if it's null, or it is a File or it's not a URL..."

So if it's null, it beeps. If it's a File, it beeps. If it's not a URL, it beeps. The only way to not get a beep there is if it's a URL.

If you want "if it's neither file nor URL", then do
if (!(obj instanceof File || obj instanceof URL)) { beep(); } 


Although, I think there's a better way to handle it than using instanceof.
 

Posts:668
Registered: 9/2/03
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:26 AM (reply 4 of 9)



 
Gah nevermind, I should pass the stream as a parameter instead of an object and let that private method determine what kind it is.

Like I said, I am being an idiot. The question of my post still remains though.
 

Posts:24,036
Registered: 2/3/03
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:27 AM (reply 5 of 9)



 
if (objType == null || objType instanceof File || !(objType instanceof URL))


Beep if:
1) the object is null
OR
2) the object is a file
OR
3) the object is not a URL
 

Posts:105
Registered: 6/8/04
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:27 AM (reply 6 of 9)



 
I think you have an error with your boolean logic--with the given code, it would beep if objType is either null, a File, or not a URL, whereas I believe you want it to beep if objType is neither a URL nor a File, correct?

Try:
if(!(objType instanceof URL || objType instanceof File))
 

Posts:668
Registered: 9/2/03
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:27 AM (reply 7 of 9)



 
If you want "if it's neither file nor URL", then do

Alright, that was it, thanks for that.

Although, I think there's a better way to handle it
than using instanceof.

Indeed, I just realised that just now.
 

Posts:24,036
Registered: 2/3/03
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:31 AM (reply 8 of 9)



 
If you want "if it's neither file nor URL", then do

Alright, that was it, thanks for that.

Although, I think there's a better way to handle it
than using instanceof.

Indeed, I just realised that just now.

Yep, use the method that takes an InputStream...

Then, you can use FileInputStream with the file, or openStream() with the URL...

 

Posts:668
Registered: 9/2/03
Re: Instance NOT an instance of an object  
Aug 6, 2004 10:34 AM (reply 9 of 9)



 
Thanks lads, I am enlightened.

P.S. Not arrogant at all! (referring to iamsomeoneelse's posts/whinings/rants)
 
This topic has 9 replies on 1 page.