You are not logged in.

#1 2010-03-28 19:17:51

something
Member
Registered: 2010-03-27
Posts: 12

[solved] Java Strange Problem.

Hey.

For class i am doing java.
anyway the stuff we are doing is really really basic and i decided to try out netbeans for my arch install.
i didnt really like the way the interface didnt fit with gtk. so after 5 mins of googling i found some stuff related to javagnome and then saw some eclipse.
I installed eclipse got all the jre/jdk + gtk stuff.
started messing around and moments later i have a MPDControl floater.

the problem is that whenever i first hit a button for say 'pause','next','prev' it doesnt work.
but the second time i click it. it works perfectly.

i implemented a hack where the program automatically calls a 'pause' when it initiates. but this doesnt fix the problem it just skips the first click so the first one the user presses actually works.

anyway heres the code for all the mpd control.
this is my first ever java programming(well besides the basic crap we do in class of . string + string, int + int basic math etc.)

//first calls seems to fail. results in BrokenPipe Exception
//subsequent calls seem to work perfectly
import java.net.UnknownHostException;
import org.bff.javampd.MPD;
import org.bff.javampd.MPDPlayer;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDPlayerException;
import org.bff.javampd.exception.MPDResponseException;

public class MPDControl {
    private MPD mServer;
    private MPDPlayer mPlayer;
    
    public void mpdConnect() {
        try {
            try {
                mServer = new MPD("127.0.0.1",6600);
            }catch(UnknownHostException e){
                System.out.println("Unknown Host");
            }
        }catch(MPDConnectionException e){
            System.out.println("Connection Error");
        }
        
        try {
            System.out.println("Version:"+mServer.getVersion());
            try {
            System.out.println("Uptime:"+mServer.getUptime());
            mServer.close();
            } catch(MPDResponseException e) {
                System.out.println("No Response");
            }
        } catch(MPDConnectionException e) {
            System.out.println("Error Connecting:"+e.getMessage());
            e.printStackTrace();
        }
    }
    
    
    
    public void mpdConnect(String nIP,int nPort) {
        try {
            try {
                mServer = new MPD(nIP,nPort);
            }catch(UnknownHostException e){
                System.out.println("Unknown Host");
            }
        }catch(MPDConnectionException e){
            System.out.println("Connection Error");
        }
        
        try {
            System.out.println("Version:"+mServer.getVersion());
            try {
            System.out.println("Uptime:"+mServer.getUptime());
            mServer.close();
            } catch(MPDResponseException e) {
                System.out.println("No Response");
            }
        } catch(MPDConnectionException e) {
            System.out.println("Error Connecting:"+e.getMessage());
            e.printStackTrace();
        }
    }
    public void mpdPlayerCreate(){
        mPlayer = mServer.getMPDPlayer();
    }
    
    public void mpdPause(){
        try {
        try {
            mPlayer.pause();
        }catch(MPDConnectionException e){
            System.out.println("Connection Error");
        }
        }catch(MPDPlayerException e){
            System.out.println("Player Exception");
        }
        
    }
    
    public void mpdNext(){
        try {
        try {
            mPlayer.playNext();
        }catch(MPDConnectionException e){
            System.out.println("Connection Error");
        }
        }catch(MPDPlayerException e){
            System.out.println("Player Exception");
        }
        
    }
    
    public void mpdPrev(){
        try {
        try {
            mPlayer.playPrev();
        }catch(MPDConnectionException e){
            System.out.println("Connection Error");
        }
        }catch(MPDPlayerException e){
            System.out.println("Player Exception");
        }
    }
}

if anyone could help me out. that would be great!! ive been enjoying the relatively productive results i have managed myself as compared to the mung that is class progress. so im definately looking to push myself further.

Last edited by something (2010-03-28 21:50:17)

Offline

#2 2010-03-28 19:21:31

kmason
Member
From: Tempe, Arizona, USA
Registered: 2010-03-23
Posts: 256
Website

Re: [solved] Java Strange Problem.

Perhaps it is because you nested the "try" statements in the functions for the buttons?

Offline

#3 2010-03-28 19:29:36

something
Member
Registered: 2010-03-27
Posts: 12

Re: [solved] Java Strange Problem.

aha!
thanks.

Offline

#4 2010-03-28 19:34:51

kmason
Member
From: Tempe, Arizona, USA
Registered: 2010-03-23
Posts: 256
Website

Re: [solved] Java Strange Problem.

No problem, sometimes you just need a second pair of eyes.

Offline

#5 2010-03-28 19:39:45

something
Member
Registered: 2010-03-27
Posts: 12

Re: [solved] Java Strange Problem.

actually the eyes that know what to look for.

anyway i changed it round a bit.
but still seem to get the problem.

import java.net.UnknownHostException;
import org.bff.javampd.MPD;
import org.bff.javampd.MPDPlayer;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDPlayerException;
import org.bff.javampd.exception.MPDResponseException;

public class MPDControl {
    private MPD mServer;
    private MPDPlayer mPlayer;
    
    public void mpdConnect() {
        try {
                mServer = new MPD("127.0.0.1",6600);
            System.out.println("Version:"+mServer.getVersion());
            System.out.println("Uptime:"+mServer.getUptime());
            mServer.close();
        }catch(MPDConnectionException e){
            System.out.println("Ex");
        }catch(UnknownHostException e){
            System.out.println("Ex");
        }catch(MPDResponseException e){
            System.out.println("Ex");
        }

    }
    
    
    
    public void mpdConnect(String nIP,int nPort) {
        try {
            mServer = new MPD(nIP,nPort);
        System.out.println("Version:"+mServer.getVersion());
        System.out.println("Uptime:"+mServer.getUptime());
        mServer.close();
    }catch(MPDConnectionException e){
        System.out.println("Ex");
    }catch(UnknownHostException e){
        System.out.println("Ex");
    }catch(MPDResponseException e){
        
    }
    }
    public void mpdPlayerCreate(){
        mPlayer = mServer.getMPDPlayer();
    }
    
    public void mpdPause(){
        try {
            mPlayer.pause();
        }catch(MPDPlayerException e){
            System.out.println("Ex");
        }catch(MPDConnectionException e){
            System.out.println("Ex");
        }
    }
    
    public void mpdNext(){
        try {
            mPlayer.playNext();
        }catch(MPDPlayerException e){
            System.out.println("Ex");
        }catch(MPDConnectionException e){
            System.out.println("Ex");
        }
    }
    
    public void mpdPrev(){
        try {
            mPlayer.playPrev();
        }catch(MPDPlayerException e){
            System.out.println("Ex");
        }catch(MPDConnectionException e){
            System.out.println("Ex");
        }
    }
}

*is that what you meant?
im very much new to this.

Last edited by something (2010-03-28 19:40:08)

Offline

#6 2010-03-28 19:45:06

something
Member
Registered: 2010-03-27
Posts: 12

Re: [solved] Java Strange Problem.

actually i just figured out what it was. i was closing the connection at the end of mpdConnect(), then when i tried to do anything else it didnt work because there was no connection the second time there is.

Offline

#7 2010-03-29 17:29:32

hBd
Member
From: Romania - Cluj Napoca
Registered: 2008-06-08
Posts: 241
Website

Re: [solved] Java Strange Problem.

i see that now, why did u do that ? tongue

Offline

Board footer

Powered by FluxBB