1

I have a Card class which contains two constructors as you can see in my code below.The issue is that each Card object takes in 2 strings and 2 images and since I wanna save those objects I have to save the images's paths as Strings and then retrieve them, thus the 2 constructors. However, using some print statements I've found out that whenever one card is retrieved, 2 separate objects are created: A normal one and another one that's null. I get that this happens because of me creating a new Card using the 1st constructor within the 2nd one. Is there a way I can avoid this so that I don't have 2 separate objects? I'd like to find a way to only use one constructor if possible.Here are my classes:

Card.java:

package com.spdesigns.dokkancardspreview.model;

import javafx.scene.image.Image;

public class Card {

private String mName;
private String mDescription;
private Image mMainImage;
private Image mSecondaryImage;
private String mMainImagePath;
private String mSecondaryImagePath;

public Card(String name, String description, Image mainImage, Image secondaryImage) {
    mName = name;
    mDescription = description;
    mMainImage = mainImage;
    mSecondaryImage = secondaryImage;
}

public Card(String name, String description , String mainImagePath, String secondaryImagePath) {
   Card newCardToAdd =  new Card(name,description,new Image(mainImagePath),new Image(secondaryImagePath));
}

@Override
public String toString() {
    return mName + " | " + mDescription;
}

public Image getmMainImage() {
    return mMainImage;
}

public Image getmSecondaryImage() {
    return mSecondaryImage;
}

public String getName() {
    return mName;
}

public String getDescription() {
    return mDescription;
}
}

home.java:

package com.spdesigns.dokkancardspreview.controllers;

import com.spdesigns.dokkancardspreview.model.Card;

import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.io.*;
import java.util.List;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class home implements Initializable {

private Card hit = new Card("Hit","Expanding Possibility",
        new Image("/images/hit_main.jpg") , new Image("/images/hit_secondary.jpg"));

private Card goku = new Card("Goku SSJ3","Everlasting legend",
        new Image("/images/gokussj3_main.jpg") , new Image("/images/gokussj3_secondary.jpg"));

private boolean clickedAgain = false;

@FXML
private Button arrowButton;
@FXML
private ImageView imageView;

@FXML
private ImageView arrow;

@FXML
private ListView listView;

protected List<Card> testingList = new ArrayList<Card>();

    protected ListProperty<Card> listProperty = new SimpleListProperty<Card>();


@Override
public void initialize(URL location, ResourceBundle resources) {
    addCard(hit);
    addCard(goku);
    //testingList.add("test2");

    listView.itemsProperty().bind(listProperty);
    // wrapping our list in an observable list and then pass that observableList to the ListProperty isntance
    listProperty.set(FXCollections.observableArrayList(testingList));

    // Handle listView selection changes
    listView.getSelectionModel().selectedItemProperty().addListener(((observable, oldValue, newValue) -> {
        Card currentlySelectedCard = listProperty.get(listView.getSelectionModel().getSelectedIndex());
        System.out.printf("ListView item clicked! Value retrieved: %s\n", currentlySelectedCard);
        imageView.setImage(new Image(currentlySelectedCard.getmMainImage().impl_getUrl()));
        arrow.setVisible(true);
        arrowButton.setVisible(true);
    }));

    arrow.translateYProperty().set(283f);
    arrowButton.translateYProperty().set(283f);
    arrow.setRotate(180);
    arrow.setVisible(false);
    arrowButton.setVisible(false);
}

public void handleShowDetails(ActionEvent actionEvent) {
    System.out.println("Button Clicked!");
    Card currentlySelectedCard = listProperty.get(listView.getSelectionModel().getSelectedIndex());
    if(clickedAgain) {
        imageView.setImage(new Image(currentlySelectedCard.getmMainImage().impl_getUrl()));
        arrow.setRotate(180);
        clickedAgain = false;
    } else {
        imageView.setImage(new Image(currentlySelectedCard.getmSecondaryImage().impl_getUrl()));
        arrow.setRotate(360);
        clickedAgain = true;
    }
}

// Saving
public void exportTo(String fileName) {
    try(
            FileOutputStream fos = new FileOutputStream(fileName);
            PrintWriter writer = new PrintWriter(fos);
    ){
        for(int i =0;i<testingList.size()-1;i++) {
            writer.printf("%s|%s|%s|%s\n",testingList.get(i).getName(),testingList.get(i).getDescription(),
                    testingList.get(i).getmMainImage().impl_getUrl(),testingList.get(i).getmSecondaryImage().impl_getUrl());
            System.out.println(testingList.get(i).toString());
        }
    } catch (IOException ioe) {
        System.out.printf("Problem saving: %s/n", fileName);
        ioe.printStackTrace();
    }
}

// Loading
public void importFrom(String fileName) {
    try(
            FileInputStream fis = new FileInputStream(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
    ){
        String line;
        while((line = reader.readLine()) != null) {
            String[] args = line.split("\\|");
            this.addCard(new Card(args[0],args[1],args[2],args[3]));
        }
    } catch (IOException ioe) {
        System.out.printf("Problem loading: %S\n" , fileName);
        ioe.printStackTrace();
    }
    int i = 0;
    for (Card card : testingList) {
        System.out.printf("%s loaded\n",testingList.get(i).toString());
        i++;
    }
    System.out.println("Loading Successful!");
}

public void addCard(Card card) {
    testingList.add(card);
}

// DEBUG purposes
public void printTestingList() {
    for (Card card : testingList) {
        System.out.println(card.toString());
    }
}
}

MAIN.JAVA:

package com.spdesigns.dokkancardspreview;

import com.spdesigns.dokkancardspreview.controllers.home;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.File;

public class Main extends Application {

private home controller;
private File file = new File("CardsCollection.txt");

@Override
public void start(Stage primaryStage) throws Exception{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/home.fxml"));
    Parent root = loader.load();
    controller = loader.getController();
    primaryStage.setTitle("Dokkan Battle Card Preview");
    primaryStage.setScene(new Scene(root, 900, 700));
    primaryStage.setResizable(false);
    // Loading cards
    primaryStage.show();
    try {
        if(!file.exists()) {
            file.createNewFile();
        }
        controller.importFrom("CardsCollection.txt");
    } catch (NullPointerException npe) {
        System.out.println("Error loading file!");
        npe.printStackTrace();
    }
}

@Override
public void stop() {
    System.out.println("App is closing!");
    // Saving before exiting
    try {
        controller.exportTo("CardsCollection.txt");
    } catch (NullPointerException npe) {
        System.out.println("Problem saving file!");
        npe.printStackTrace();
    }
   // controller.printTestingList();
}

public static void main(String[] args) {
    launch(args);
}
}
Stelios Papamichail
  • 955
  • 2
  • 19
  • 57

3 Answers3

1

change

public Card(String name, String description , String mainImagePath, String secondaryImagePath) {
   Card newCardToAdd =  new Card(name,description,new Image(mainImagePath),new Image(secondaryImagePath));
}

to

public Card(String name, String description , String mainImagePath, String secondaryImagePath) {
   this(name,description,new Image(mainImagePath),new Image(secondaryImagePath));
}
toongeorges
  • 1,844
  • 17
  • 14
  • Note that this solution leaves `mMainImagePath` and `mSecondaryImagePath` fields uninitialized. – Mick Mnemonic Sep 19 '17 at 11:57
  • @MickMnemonic yes, but these fields are private and not accessible, maybe these fields should be removed or if they are to be used, they should have a getter? – toongeorges Sep 19 '17 at 12:03
0

the problem with your code is in this constructor:

public Card(String name, String description , String mainImagePath, String secondaryImagePath) {
   Card newCardToAdd =  new Card(name,description,new Image(mainImagePath),new Image(secondaryImagePath));
}

as you see, you create a new card newCardToAdd in there, leaving the new instance completely wrong initialized...

what you do in such cases is call one constructor from another one... in your case your 2 cosntructors should look like:

public Card(String name, String description, Image mainImage, Image secondaryImage) {
    mName = name;
    mDescription = description;
    mMainImage = mainImage;
    mSecondaryImage = secondaryImage;
}

public Card(String name, String description, String mainImagePath, String secondaryImagePath) {
    this(name, description, new Image(mainImagePath), new Image(secondaryImagePath));
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

If you never need to pass Images directly to the class (only image paths), a single constructor is enough:

public Card(String name, String description, 
               String mainImagePath, String secondaryImagePath) {
    mName = name;
    mDescription = description;
    mMainImagePath = mainImagePath;
    mSecondaryImagePath = secondaryImagePath;
    mMainImage = new Image(mainImagePath);
    mSecondaryImage = new Image(secondaryImagePath);
}
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30