Grails NumberFormatException

Grails NumberFormatException,grails,groovy,cron,Grails,Groovy,Cron,我在我的Grails代码中收到NumberFormatException错误,该代码用于将电影排序到数据库中。错误表明它来自cron插件。我已经做了研究,我一直试图通过使用NumberFormatException捕捉错误,但没有成功。我认为问题在于索引服务。任何帮助都将不胜感激 确切的错误是: 2014-07-25 10:09:07,779 [quartzScheduler_Worker-1] ERROR listeners.ExceptionPrinterJobListener - Exc

我在我的
Grails
代码中收到
NumberFormatException
错误,该代码用于将电影排序到数据库中。错误表明它来自
cron
插件。我已经做了研究,我一直试图通过使用
NumberFormatException
捕捉错误,但没有成功。我认为问题在于
索引服务
。任何帮助都将不胜感激

确切的错误是:

2014-07-25 10:09:07,779 [quartzScheduler_Worker-1] ERROR listeners.ExceptionPrinterJobListener  - Exception occurred in job: Grails Job
Message: java.lang.NumberFormatException: For input string: "N/A" 
我正在使用:

Grails version: 2.4.2
Groovy version: 2.3.3
JVM version: 1.7.0_51
quartz - 1.0.2
我的代码:

索引服务

package movie

import groovy.io.FileType
import groovy.json.JsonSlurper

class IndexService {

    static transactional = false

    def grailsApplication
    private cleanText(String title)
    {
        //split string into array via .,+,[,] etc...
        def splitTitle=(title.toLowerCase()).tokenize('+.[]!£$%^+=~#:; \t \n \r \f / \\ { } @ & - ( )')
        //Get rid of extention
        splitTitle.remove(splitTitle.size()-1)

        //Get rid of Dvdrip and author
        //unwanted phrases
        def unwanted=["dvdrip","eng","www","torentz","3xforum","bugz","ro","maxspeed","xvid","720p","bluray","yify","1080p","hd","x264","RARBG","mp3","mp4","flv","brrip","rip"]
        //Get rid of dates
        //get 2000 to current date to put into the unwanted list
        for(i in 2000..3000){unwanted.add(i.toString())}
        //def empty string to put all our cleaned up text into
        def cleanedText = ""
        //cleaning up word mess...
        splitTitle.each {word->
            if(!unwanted.contains(word))
            {
                cleanedText=cleanedText+word+" "
            }
        }
        //returning with +'s
        return (cleanedText[0..-2]).replaceAll("\\s","+")
    }

    def getInfo(String title)
    {
        //returned nice with +
        def cleanTitle=cleanText(title)
        //define my reader
        def scan = new JsonSlurper()
        def scanner =scan.parseText(("http://www.omdbapi.com/?i=&plot=full&t="+cleanTitle).toURL().text)
        if(scanner.Response=="True")
        {
            /*
            organisied like this:
            String artwork
            String title
            Date dateReleased
            String userRated
            String lengthOfFilm
            String genre
            String plot
            float rating
 */         //returns our info we have scraped
            return [
                    true,
                    scanner.Poster,
                    scanner.Title,
                    new Date(scanner.Released),
                    scanner.Rated,
                    scanner.Runtime,
                    scanner.Genre,
                    scanner.Plot,
                    scanner.imdbRating.toFloat()/10
            ]
        }
        else
        {
            return [null,cleanTitle]
        }


    }
    def indexFiles()
    {
        //returns fileLocation
        def fileLocation=grailsApplication.config.grails.movie.location
        //Setup as file object
        def dir = new File(fileLocation)
        //recurse all files
        dir.eachFileRecurse (FileType.FILES) { file ->

            println(file)
            //only create a record if no file test if record exists
            if(!Record.findByPathToFile(file.getCanonicalPath())) {
                //get record propreties set
                def record = new Record()
                record.pathToFile = file.getCanonicalPath()
                //call to get data of a film returns as a list
                def info = getInfo(file.getName())
                //check if everthing is alright
                info.each{print(it)}
                if (info[0] == null) {
                    try {
                        //set fallback propeties
                        record.artwork = null
                        record.title = info[1].replaceAll("\\+"," ")
                        record.genre = null
                        record.dateReleased = new Date(file.lastModified())
                        record.lengthOfFilm = null
                        record.plot = null
                        record.rating = 0
                        record.userRated = null
                        record.save(failOnError: true)
                    }
                    catch (NumberFormatException e) {
                        //catch any errors
                        log.error("Error caught :${e} \n")
                    }
                }
                else
                {
                    try
                    {
                        //set good propeties
                        record.artwork = info[1]
                        record.title = info[2]
                        record.genre = info[6]
                        record.dateReleased = info[3]
                        record.lengthOfFilm = info[5]
                        record.plot = info[7]
                        print(info[8])
                        record.rating = info[8]
                        record.userRated = info[4]
                        record.save(failOnError: true)
                    }
                    catch (NumberFormatException e) {
                        //catch any errors
                        info.each
                                {
                                    print(it)
                                }
                        log.error("Error caught :${e} \n")
                    }
                }
            }
        }
    }
}
记录域

package movie

class Record {
    //static searchable = true
    //data will be recieved via http://www.omdbapi.com/
    String artwork
    String title
    Date dateReleased
    String pathToFile
    String userRated
    String lengthOfFilm
    String genre
    String plot
    float rating

    static constraints = {
        artwork nullable: true
        title nullable: false, unique: true
        dateReleased nullable: false
        pathToFile nullable: false
        userRated nullable: true
        lengthOfFilm nullable: true
        genre nullable: true
        plot nullable: true, maxSize: 2000
        rating nullable: true
    }
}
最后是IndexJob

package movie



class IndexJob {
    static triggers = {
        /*
    cronExpression: "s m h D M W Y"
                     | | | | | | `- Year [optional]
                     | | | | | `- Day of Week, 1-7 or SUN-SAT, ?
                     | | | | `- Month, 1-12 or JAN-DEC
                     | | | `- Day of Month, 1-31, ?
                     | | `- Hour, 0-23
                     | `- Minute, 0-59
                     `- Second, 0-59
     */

        cron name: "indexTrigger", cronExpression: "0  0/1 * * * ? *"
    }
    def indexService

    def execute() {
        indexService.indexFiles()

    }
}
试试这个:

package movie

import groovy.io.FileType
import groovy.json.JsonSlurper

class IndexService {

    static transactional = false

    def grailsApplication
    private cleanText(String title)
    {
        //split string into array via .,+,[,] etc...
        def splitTitle=(title.toLowerCase()).tokenize('+.[]!£$%^+=~#:; \t \n \r \f / \\ { } @ & - ( )')
        //Get rid of extention
        splitTitle.remove(splitTitle.size()-1)

        //Get rid of Dvdrip and author
        //unwanted phrases
        def unwanted=["dvdrip","eng","www","r5","unique","torentz","3xforum","bugz","ro","maxspeed","xvid","720p","bluray","yify","1080p","hd","x264","RARBG","mp3","mp4","flv","brrip","rip"]
        //Get rid of dates
        //get 2000 to current date to put into the unwanted list
        for(i in 2000..3000){unwanted.add(i.toString())}
        //def empty string to put all our cleaned up text into
        def cleanedText = ""
        //cleaning up word mess...
        splitTitle.each {word->
            if(!unwanted.contains(word))
            {
                cleanedText=cleanedText+word+" "
            }
        }
        //returning with +'s
        return (cleanedText[0..-2]).replaceAll("\\s","+")
    }

    def getInfo(String title)
    {
        //returned nice with +
        def cleanTitle=cleanText(title)
        //define my reader
        def scan = new JsonSlurper()
        def scanner =scan.parseText(("http://www.omdbapi.com/?i=&plot=full&t="+cleanTitle).toURL().text)
        if(scanner.Response=="True")
        {
            /*
            organisied like this:
            String artwork
            String title
            Date dateReleased
            String userRated
            String lengthOfFilm
            String genre
            String plot
            float rating
 */         //returns our info we have scraped
            def imdb = scanner.imdbRating
            if (imdb.equalsIgnoreCase("n/a")) {
                imdb = "0"
            }

            return [
                    true,
                    scanner.Poster,
                    scanner.Title,
                    new Date(scanner.Released),
                    scanner.Rated,
                    scanner.Runtime,
                    scanner.Genre,
                    scanner.Plot,
                    imdb.toFloat()/10
            ]
        }
        else
        {
            return [null,cleanTitle]
        }


    }
    def indexFiles()
    {
        //returns fileLocation
        def fileLocation=grailsApplication.config.grails.movie.location
        //Setup as file object
        def dir = new File(fileLocation)
        //recurse all files
        dir.eachFileRecurse (FileType.FILES) { file ->
            //only create a record if no file test if record exists
            if(Record.findByPathToFile(file.getCanonicalPath())==null) {
                //get record propreties set
                def record = new Record()
                record.pathToFile = file.getCanonicalPath()
                //call to get data of a film returns as a list
                def info = getInfo(file.getName())
                //check if everthing is alright
                if (info[0] == null) {
                    try {
                        //set fallback propeties
                        record.artwork = null
                        record.title = info[1].replaceAll("\\+"," ")
                        record.genre = null
                        record.dateReleased = new Date(file.lastModified())
                        record.lengthOfFilm = null
                        record.plot = null
                        record.rating = 0
                        record.userRated = null
                        record.save(failOnError: true)
                    }
                    catch (Exception e) {
                        //catch any errors
                        //log.error("Error caught :${e} \n")
                    }
                }
                else
                {
                    try
                    {
                        //set good propeties
                        record.artwork = info[1]
                        record.title = info[2]
                        record.genre = info[6]
                        record.dateReleased = info[3]
                        record.lengthOfFilm = info[5]
                        record.plot = info[7]
                        record.rating = info[8]
                        record.userRated = info[4]
                        record.save(failOnError: true)
                    }
                    catch (Exception e) {
                        catch any errors
                        log.error("Error caught :${e} \n")
                    }
                }
            }
        }
    }
}

我花了一个小时才弄明白

您发布了太多不相关的代码,很难说出您做错了什么。哪一行抛出异常?哦,对不起,忘记解释了,我看到你有NumberFormatException错误,所以我认为它一定是来自你导入的浮点,所以我所做的就是捕获它并将其设置为0。对不起,我是这个stackoverflow系统的新手!