JavaFX show stage

时间:2015-10-19 10:35:44

标签: eclipse netbeans javafx fxml efxclipse

我将一个JavaFX项目从NetBeans导入Eclipse。奇怪的是,我无法执行在NetBeans中运行良好的代码。我用SceneBuilder设置了一个小gui。我只想展示一点.fxml - 此时根本没有功能代码。我的主要课程如下:

public class Main extends Application {

public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle(Strings.appName);
        stage.getIcons().add(new Image("sql.png")); 
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

但是当我尝试执行代码时,eclipse编译器返回:

  

Application start方法中的异常   java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(未知   来源)at   com.sun.javafx.application.LauncherImpl.launchApplication(未知   来源)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native   方法)at sun.reflect.NativeMethodAccessorImpl.invoke(未知   来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知   来自)java.lang.reflect.Method.invoke(未知来源)at   sun.launcher.LauncherHelper $ FXHelper.main(Unknown Source)引起:   java.lang.RuntimeException:Application start方法中的异常   com.sun.javafx.application.LauncherImpl.launchApplication1(未知   来源)at   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 156(未知   java)在java.lang.Thread.run(未知来源)引起:   java.lang.NullPointerException:位置是必需的。在   javafx.fxml.FXMLLoader.loadImpl(未知来源)at   javafx.fxml.FXMLLoader.loadImpl(未知来源)at   javafx.fxml.FXMLLoader.loadImpl(未知来源)at   javafx.fxml.FXMLLoader.loadImpl(未知来源)at   javafx.fxml.FXMLLoader.loadImpl(未知来源)at   javafx.fxml.FXMLLoader.load(未知来源)at   core.Main.start(Main.java:27)at   com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 163(未知   来源)at   com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 176(未知   来源)at   com.sun.javafx.application.PlatformImpl.lambda为$ null $ 174(未知   来自java.security.AccessController.doPrivileged(Native Method)     在   com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 175(未知   来源)com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(未知   来源)com.sun.glass.ui.win.WinApplication._runLoop(Native   方法)at   com.sun.glass.ui.win.WinApplication.lambda $ null $ 149(未知来源)     ... 1个异常运行应用程序核心。主要

这是我的项目结构:

SQL
│   .classpath
│   .project
│
├───.settings
│       org.eclipse.jdt.core.prefs
│
├───bin
│   ├───gui
│   │       FXMLDocument.fxml
│   │       FXMLDocumentController.class
│   │       Main.class
│   │       sql.png
│   │
│   ├───print
│   │       Allgemein.class
│   │       Mahnung.class
│   │       PDF.class
│   │       Rechnung.class
│   │
│   └───various
│           Strings.class
│
└───src
    └───gui
            FXMLDocument.fxml
            FXMLDocumentController.java
            Main.java

我在这里做错了什么?或者是eclipse在这里期待什么,NetBeans不是吗?!?

1 个答案:

答案 0 :(得分:0)

就像@James_D建议的那样,我能够使用

来追踪问题
import UIKit
import GoogleMaps

class ViewController: UIViewController, NSXMLParserDelegate {

    var parser = NSXMLParser()
    var timer  = NSTimer()

    // viewDidLoad()
    override func viewDidLoad() {
        super.viewDidLoad()

        // runs viewDidLoad() every 10 seconds
        timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "viewDidLoad", userInfo: nil, repeats: true)

        // XML file
        parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://link.to.xml.file.xml"))!)!

        let coord = Coord2()
        parser.delegate = coord
        parser.parse()
        print("coord has a count attribute of \(coord.count)")
        print("coord has \(coord.markers.count) markers")

        // displays the map adjusted to UC Santa Cruz
        let camera = GMSCameraPosition.cameraWithLatitude(37.0000,
        longitude: -122.0600, zoom: 14)
        let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.mapType = kGMSTypeNormal
        mapView.myLocationEnabled = true
        self.view = mapView

        // loops through all the lats and lngs of the buses and produces a marker
        // for them on our Google Maps app
        for marker in coord.markers {
            print("marker id=\(marker.id), lat=\(marker.lati), lng=\(marker.lngi), route=\(marker.route)")

            // displays the buses
            let buses = GMSMarker()
            buses.position = CLLocationCoordinate2DMake(marker.lati, marker.lngi)
            buses.title = marker.route
            if buses.title == "UPPER CAMPUS" {
                buses.icon = UIImage(named: "uppercampus")
            } else if buses.title == "LOOP" {
                buses.icon = UIImage(named: "innerloop")
            }
            buses.snippet = marker.id
            buses.map = mapView
        }
    }

    // didReceiveMemoryWarning()
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

// ParseBase class
// simple base class that is used to consume foundCharacters
// via the parser
class ParserBase : NSObject, NSXMLParserDelegate  {

    var currentElement:String = ""
    var foundCharacters = ""
    weak var parent:ParserBase? = nil

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        currentElement = elementName
    }

    func parser(parser: NSXMLParser, foundCharacters string: String) {
        self.foundCharacters = string
    }

}

// Coord2 class
// represents a coord2 tag
// it has a count attribute
// and a collection of markers
class Coord2 : ParserBase {

    var count = 0
    var markers = [Marker]()

    // didStartElement()
    override func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

        print("processing <\(elementName)> tag from Coord")

        if elementName == "coord2" {

            // if we are processing a coord2 tag, we are at the root
            // of XML file, extract the count value and set it
            print(attributeDict["count"])
            if let c = Int(attributeDict["count"]!) {
                self.count = c
            }
        }

        // if we found a marker tag, delegate further responsibility
        // to parsing to a new instance of Marker
        if elementName == "marker" {
            let marker = Marker()
            self.markers.append(marker)

            // push responsibility
            parser.delegate = marker

            // let marker know who we are
            // so that once marker is done XML processing
            // it can return parsing responsibility back
            marker.parent = self
        }
    }
}

// Marker class
class Marker : ParserBase {

    var id = ""
    var lat = ""
    var lng = ""
    var route = ""
    var lati = 0.0
    var lngi = 0.0

    // didEndElement()
    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {

        print("processing <\(elementName)> tag from Marker")

        // if we finished an item tag, the ParserBase parent
        // would have accumulated the found characters
        // so just assign that to our item variable
        if elementName == "id" {
            self.id = foundCharacters
        }

        // convert the lat to a Double
        else if elementName == "lat" {
            self.lat = foundCharacters
            // cast self.lat as Double
            if let doubleFromlat = Double(self.lat) {
                self.lati = doubleFromlat
            } else { print("foundCharacters for lat does not hold double") }

        }

        // convert the lng to a Double
        else if elementName == "lng" {
            self.lng = foundCharacters
            if let doubleFromlng = Double(self.lng) {
                self.lngi = doubleFromlng
            } else { print("foundCharacters for lng does not hold double") }
        }

        else if elementName == "route" {
            self.route = foundCharacters
        }

        // if we reached the </marker> tag, we do not
        // have anything further to do, so delegate
        // parsing responsibility to parent
        else if elementName == "marker" {
            parser.delegate = self.parent
        }

        // reset found characters
        foundCharacters = ""
    }

}

System.out.println(getClass().getResource("Main.class"));

当我从运行NetBeans的ubuntu笔记本电脑复制到运行10和Eclipse的主Windows机器时,我的类路径必须以某种方式损坏。我修好了,现在一切正常。