0

I am creating a program which reads a CSV file (article numbers) and searches web pages (e.g. asdfjh.com/12547.hmtl, asdfjh.com/12548.hmtl, asdfjh.com/12549.hmtl, asdfjh.com/12550.hmtl).

The purpose is to grab the eans on the sites and add them to the CSV-file. I'm having a problem that I don't understand.

Code:

package beginnDesignUndFunktion.code;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class DesignController {
    static File file;

    @FXML
    ProgressBar progressBar = new ProgressBar();

    @FXML
    protected void handleButtonAction(ActionEvent event) {
        FileChooser chooser = new FileChooser();
    chooser.setTitle("Open File");
    file = chooser.showOpenDialog(new Stage());
    System.out.println(file.toString());
    try {
        int anzahl = CsvVerabeitung.countLines(file.toString());
        System.out.println(anzahl + " Dateien");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@FXML
protected void handleSubmitButtonAction(ActionEvent event) {
    String path = file.toString();
    Reader.read(path);
    Writer.write();
    }

    @FXML
    protected static void setProgress(int y){
        int anzahl = 0;
        try {
            anzahl = CsvVerabeitung.countLines(file.toString());    
        } catch (IOException e) {
            e.printStackTrace();
        }
        double progress = ((double) y) / ((double) anzahl);
        System.out.println(progress);
        progressBar.setProgress(progress);
    }
}

The communication with other classes works. What am I doing wrong?

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
Wayne
  • 1
  • 1
  • 1
    `setProgress` is `static` but `progressBar` is not. – Slaw Oct 05 '18 at 04:19
  • 1
    Possible duplicate of [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Slaw Oct 05 '18 at 04:20
  • [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) – Rafael Oct 05 '18 at 04:27
  • 1
    dont use the static modifier anywhere ... – kleopatra Oct 05 '18 at 07:52
  • 1
    unrelated to your problem: please learn java naming conventions and stick to them – kleopatra Oct 05 '18 at 07:53
  • Ok now its like this: – Wayne Oct 06 '18 at 23:35

2 Answers2

0

Ok now its like this:

package beginnDesignUndFunktion.code;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class DesignController {
    File file;
    CsvVerabeitung c = new CsvVerabeitung();
    Reader r = new Reader();
    Writer w = new Writer();

    @FXML
    ProgressBar progressBar = new ProgressBar();

    @FXML
    protected void handleButtonAction(ActionEvent event) {
        FileChooser chooser = new FileChooser();
        chooser.setTitle("Open File");
        file = chooser.showOpenDialog(new Stage());
        System.out.println(file.toString());
        try {
            int anzahl = c.countLines(file.toString());
            System.out.println(anzahl + " Dateien");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    protected void handleSubmitButtonAction(ActionEvent event) {
        String path = file.toString();
        r.read(path);
        w.write();
    }

    protected void setProgress(int y){
        int anzahl = 0;
        try {
            anzahl = c.countLines(file.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        double progress = ((double) y) / ((double) anzahl);
        System.out.println(progress);
        progressBar.setProgress(progress);
    }
}

and:

package beginnDesignUndFunktion.code;

import java.io.IOException;

public class Writer extends DesignController{

    Reader r = new Reader();
    CsvVerabeitung c = new CsvVerabeitung();
    DesignController d = new DesignController();

    protected void write() {
        Reader r1 = new Reader();
        String path = "C:\\Users\\marco\\Desktop\\CSV Verarbeitung\\src\\beginnDesignUndFunktion\\data\\test7Files2.csv";
        java.io.BufferedWriter FileWriter;

        {
            try {
                FileWriter = new java.io.BufferedWriter(new java.io.FileWriter(new java.io.File(path)));
                FileWriter.write("id;ean");
                int i=0;
                for(String x: r.number){
                    FileWriter.append("\n" + r.number.get(i) + ";" + r.ean.get(i) + ";");
                    i++;
                    d.setProgress(i);
                }
                FileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Problem now: java.lang.StackOverflowError

How made for this site :DDDD

Really had to laugh!

btw @kleopatra: will lock up the conventions. @slaw: progressBar doesn´t work it its static.

EDIT

The problem is the d.setProgress(i); call.
Wayne
  • 1
  • 1
  • this is not an answer, is it? Instead of polluting the answer section please edit your question with new/relevant detail ... good, looking forward to seeing code that's sticking to conventions :) – kleopatra Oct 07 '18 at 13:09
0

SOLVED with displeasure...

Copied the code of Writer-Class and put it into the Controller-Class.

Not a nice solution but it works

Wayne
  • 1
  • 1