0

EDIT:

Sorry forr the misspellings and typos, I didn't want to put my code here so I tried to make a new look a like code to express my question.

Here is the actual code I'm using, I just removed some parts of it as they are not related to my question, I think, otherwise just ask me and I'll put it here as well.

Heres the actual code:

public class Dados {

    private String sta;
    private String ap;
    private int startTime;
    private int endTime;
    private int repetitionSTA;
    private int pingPong;
    private int tt_previous;
    private int tt_next;
    private int id;

    public Dados(int id, int startTime, int endTime, String ap, String sta, int repetitionSTA, int ttprevious, int ttnext, int ppong)
    {
        this.sta = sta;
        this.ap = ap;
        this.startTime = startTime;
        this.endTime=endTime;
        this.pingPong = ppong;
        this.tt_next = ttnext;
        this.tt_previous = ttprevious;
        this.id = id;
        this.repetitionSTA = repetitionSTA;
    }

    // SET

    public void setPingPong()
    {
        this.pingPong = 1;
    }


    //GET

    public int getPingPong()
    {
        return this.pingPong;
    }

}

//another class from now on

public class Queries extends LigarBD{

    String dbtime = null; 

    int id = 1;

    TreeMap<Integer, ArrayList> tmValores = new TreeMap<>();
    ArrayList<Dados> listaObjectos = new ArrayList<>();
    ArrayList<Dados> listaObjectos2 = new ArrayList<>();


    public ArrayList getUniqueStations(String server)
    {
        ArrayList<String> listaSTA = new ArrayList<>();

        String query = "SELECT distinct calling_station_id FROM java_logs;";

        try
        {
            super.ligar(server);
            Statement s = super.getConexao().createStatement();
            ResultSet rs = s.executeQuery(query);
            while (rs.next())
            {
                listaSTA.add(rs.getString(1));
            }
            rs.close();
            s.close();
            super.desligar(super.getConexao());

        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error at listing all unique stations. Reason -> "+e.getMessage());
            System.out.println("Error at listing all unique stations. Reason ->  "+e.toString());
        }

        return listaSTA;
    }


    public ArrayList getStationData(String mac, String server)
    {

        try 
        {
            super.ligar(server);
            Statement s = getConexao().createStatement();

            ResultSet rs = s.executeQuery("SELECT timestamp-acct_session_time, timestamp, called_station_id, calling_station_id "
                                        + "FROM java_logs where calling_station_id = '"+mac+"';"); // retirar STA da query *******************
            //System.out.println("Executing the Query on+"+server+" - UniqueSTA - Query number: 1?");
            int repetitionSTA=1;
            while (rs.next()) 
            {              
                Dados d = new Dados(id, rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), repetitionSTA, 0, 0, 0);

                listaObjectos2.add(d);
                repetitionSTA++;
                id++;
            }

            rs.close();
            s.close();
            super.desligar(super.getConexao());
        }
        catch (Exception e) 
        {
            JOptionPane.showMessageDialog(null,"Error at Select Query. Reason -> "+e.getMessage());
        }

        return listaObjectos2;
    }

}

Another class:

public class Pingpong {

    ArrayList<Dados> dadosArray = new ArrayList<>();



    Queries q = new Queries();

    TreeMap<Integer, ArrayList> mapa = new TreeMap<>();
    ArrayList<Dados> arrayDeDados = new ArrayList<>();


    public ArrayList detectPingPongArray(int threshold_access_session_time, int threshold_transition_time, ArrayList<Dados> dadosSTA)
    {
        dadosArray=dadosSTA;
        for(int i = 1; i<arrayDeDados.size()-1; i++)
        {
            dadosArray.get(i).setPingPong();
        }
        return dadosArray;
    }


}

And here is where I'm printing each object one by one:

ArrayList<Dados> dadosSTA = new ArrayList<>();
        ArrayList<Dados> dataForPPong = new ArrayList();

        ArrayList uniqueSTA = q.getUniqueStations("localserver");

        for(int i = 0; i<uniqueSTA.size(); i++)
        {
            dadosSTA = q.getStationData(uniqueSTA.get(i).toString(), "localserver");
            dataForPPong = p.detectPingPongArray(5, 3, dadosSTA);

        }

        for(int i=0; i<dataForPPong.size(); i++)
        {
            System.out.println("ID: "+dataForPPong.get(i).getId()+" STA: "+dataForPPong.get(i).getStation()
                    + " PingPong: "+dataForPPong.get(i).getPingPong());
        }

So I was expecting it to change the value of pingPong in all objects to 1 but it doesn't.

I think the problem is with the returning from the method detectPingPongArray but I don't know where is the mistake.

Anyone can pinpoint the problem here?


Cannot access non-static method in static context?

Given this code....

public class CalibrationViewModel : ViewModelBase
{
    private FileSystemWatcher fsw;

    public CalibrationViewModel(Calibration calibration)
    {
        fsw = new FileSystemWatcher
            {
                Path = @"C:\Users\user\Desktop\Path\ToFile\Test_1234.txt",
                Filter = @"Test_1234.txt",
                NotifyFilter = NotifyFilters.LastWrite
            };

        fsw.Changed += (o, e) =>
            {
                var lastLine = File.ReadAllLines(e.FullPath).Last();
                Dispatcher.BeginInvoke((Action<string>) WriteLineToSamplesCollection, lastLine); //line that cites error
            };
    }

    private void WriteLineToSamplesCollection(string line)
    {
        // do some work
    }
}

Why am I getting the error, 'Cannot access non-static method BeginInvoke in static context'?

I have looked at several other examples on SE and most cite trying to use a field before the object is created as if they were trying to use a non-static field in a static manner, but I don't understand what it is about my code that is invoking the same error.

Lastly, what can I do to fix this specific issue/code?

Update: Fixed title to reflect issue with a 'method' and not a 'property'. I also added that the class implements ViewModelBase.

4

1 回答 1

3

问题

我想说您的代码中有一些不好的做法,例如忽略ArrayLists 中的泛型,但让我们切入正题。

在我看来,您的问题出在以下方法中:

public ArrayList detectPingPongArray(
        int threshold_access_session_time,
        int threshold_transition_time,
        ArrayList<Dados> dadosSTA
) {
    dadosArray=dadosSTA;
    for(int i = 1; i<arrayDeDados.size()-1; i++) {
        dadosArray.get(i).setPingPong();
    }
    return dadosArray;
}

这是您的代码,只是使用不同的格式来适应答案。

该方法接收一个ArrayList<Dados> dadosSTA,您将其分配给dadosArray
您返回相同的变量,并且想要对其进行修改。

但是,您正在迭代arrayDeDados的大小,这是一个不同的 ArrayList<Dados>,并且,从您给我们的内容来看,它也是一个列表。

ArrayList<Dados> arrayDeDados = new ArrayList<>();

因此,由于size()空列表的 为零,因此不执行迭代,并且setPingPong()永远不会调用。


提示

根据要求,我还为未来添加了一些提示。

  1. 虽然不一定是坏习惯,更多的是个人喜好,但我不会用葡萄牙语(在这种情况下似乎是这样)或除英语以外的任何语言命名我的类/变量。在这种情况下,它使代码对其他人更具可读性。

  2. public class Queries extends LigarBD
    我不确定在数据库上执行查询的类是否应该扩展连接到数据库的类。相反,使用连接到数据库的类似乎更合适。

    您的代码中的某些模式很容易看到这一点,例如super.ligar(), super.getConexao(), super.desligar(),您在与我们共享的两种方法中都这样做了。您似乎对使用 提供的接口感兴趣LigarBD,而不是对其进行扩展或添加功能。您可以通过声明 type 的实例变量LigarBD并相应地使用它来更改它。

  3. public ArrayList detectPingPongArray
    在这里,您丢弃了与ArrayList. 如果您知道您将返回一个ArrayList<Dados>,并且您希望此方法的调用者也知道这一点,则应按如下方式声明该方法:
    public ArrayList<Dados> detectPingPongArray

    这样,调用者将期望一个ArrayList<Dados>,而不是ArrayList 某物(可能会引入不安全的强制转换/ 操作)。


进一步的分析

我也不确定这是否是故意的,但我在你的代码中发现了一些相当奇怪的东西。

ArrayList<Dados> dadosSTA = new ArrayList<>();
ArrayList<Dados> dataForPPong = new ArrayList();

ArrayList uniqueSTA = q.getUniqueStations("localserver");

for(int i = 0; i<uniqueSTA.size(); i++)
{
    dadosSTA = q.getStationData(uniqueSTA.get(i).toString(), "localserver");
    dataForPPong = p.detectPingPongArray(5, 3, dadosSTA);
}

for(int i=0; i<dataForPPong.size(); i++)
{
    System.out.println("ID: "+dataForPPong.get(i).getId()+" STA: "+dataForPPong.get(i).getStation()
            + " PingPong: "+dataForPPong.get(i).getPingPong());
}

第一个for循环只是为变量分配新值,对它们不做任何事情并不断覆盖它们。

可能,您希望此循环还包括第二个循环,以便您有效地打印所有值,对于每个ArrayList分配给dataForPPong. 或者你将来会在这个循环中添加其他东西,但我想指出这可能是未来的错误来源。

于 2013-06-03T20:38:48.367 回答