Runtime Nill Error

Hi


This is a simple Core Data project I dont know why Im getting nil value error in line 46 in View Controller although everyting seems to be fine !

--

Kindest Regards


h ttps://www.dropbox.com/s/mummh4429nkhs3h/CoreDataApp.zip?dl=0

Answered by Claude31 in 415428022

Beyond this crash, you have an error in the handling of tableView.

You set number of sections to 0 (hence nothing is displayed).

Must be 1 or more.

    func numberOfSections(in tableView: UITableView) -> Int {
           return 1
       }

There is no line 46. It is just an empty line, beyond the end of the code (line 44).

So, I tested, it is line 39.


It is good to give access to the full project, it would also save our time to copy the relevant class (here it is a small one)


The crash comes from:

            self.studentList! = try self.context!.fetch(studentFetch) as [Student]

You unwrap self.studentList but is is still nil.


Replace by

            self.studentList = try self.context!.fetch(studentFetch) as [Student]


Note that

as [Student]

is superfluous.

Accepted Answer

Beyond this crash, you have an error in the handling of tableView.

You set number of sections to 0 (hence nothing is displayed).

Must be 1 or more.

    func numberOfSections(in tableView: UITableView) -> Int {
           return 1
       }

Thank you very much

Runtime Nill Error
 
 
Q